Java Programming - Threads - Discussion

Discussion Forum : Threads - Finding the output (Q.No. 6)
6.
What will be the output of the program?
class s1 extends Thread
{ 
    public void run() 
    { 
        for(int i = 0; i < 3; i++) 
        { 
            System.out.println("A"); 
            System.out.println("B"); 
        } 
    } 
} 
class Test120 extends Thread 
{ 
    public void run() 
    { 
        for(int i = 0; i < 3; i++) 
        { 
            System.out.println("C"); 
            System.out.println("D"); 
        } 
    } 
    public static void main(String args[]) 
        { 
        s1 t1 = new s1(); 
        Test120 t2 = new Test120(); 
        t1.start(); 
        t2.start(); 
    } 
}
Compile time Error There is no start() method
Will print in this order AB CD AB...
Will print but not be able to predict the Order
Will print in this order ABCD...ABCD...
Answer: Option
Explanation:

We cannot predict the order in which threads are going to run.

Discussion:
7 comments Page 1 of 1.

SAYYED NURJAHAN said:   3 years ago
Can anyone tell me, How can we make thread class object?

Naresh said:   6 years ago
It's work simply a normal program. If we want to run it as a separate thread then We should make a thread class object and pass a class reference in Thread class constructor.

Prashantp224 said:   6 years ago
I got an output as,

A
B
A
B
A
B
C
D
C
D
C
D

Nik said:   7 years ago
Without thread object can a start method be called?

Prashant said:   7 years ago
A
B
A
B
A
B
C
D
C
D
C
D
We got this output because of there no make a Thread class object. It's work simply a normal program. We should make a thread class object and pass a class reference in Thread class constructor.

Rajesh said:   9 years ago
Yep.. me too got the same output.
A
B
A
B
A
B
C
D
C
D
C
D

Its easily predictable.

Deepss said:   1 decade ago
I got following output!!...
A
B
A
B
A
B
C
D
C
D
C
D
The output is very much predictable ,the threads are being called on seperate instances t1 and t2...so first start method of t1thread will be called its run executed then followed by start of t2 thread.

Post your comments here:

Your comments will be displayed after verification.