Java Programming - Threads - Discussion

14. 

What will be the output of the program?

class Test116 
{ 
static final StringBuffer sb1 = new StringBuffer(); 
static final StringBuffer sb2 = new StringBuffer(); 
public static void main(String args[]) 
{ 
    new Thread() 
    { 
        public void run() 
        { 
            synchronized(sb1) 
            { 
                sb1.append("A"); 
                sb2.append("B"); 
            } 
        } 
    }.start(); 

    new Thread() 
    { 
        public void run() 
        { 
            synchronized(sb1) 
            { 
                sb1.append("C"); 
                sb2.append("D"); 
            } 
        } 
    }.start(); /* Line 28 */

    System.out.println (sb1 + " " + sb2); 
    } 
}

[A]. main() will finish before starting threads.
[B]. main() will finish in the middle of one thread.
[C]. main() will finish after one thread.
[D]. Cannot be determined.

Answer: Option D

Explanation:

Can you guarantee the order in which threads are going to run? No you can't. So how do you know what the output will be? The output cannot be determined.

add this code after line 28:

try { Thread.sleep(5000); } catch(InterruptedException e) { }

and you have some chance of predicting the outcome.


Rohan said: (Feb 9, 2012)  
I will try this program main thread always execute first after that other 2 thread execute. Please give me proper answer.

Poonam said: (May 14, 2012)  
Can anyone explain me that how this output came. Because program executes from main and when the main get closed then why is it considering the lines outside the main.

Vishal Bhatt said: (Jan 21, 2013)  
Only one thread is able to start. And output is AB.

So anyone can tell me about how to generate this output?

Srikanth said: (Jun 26, 2013)  
Whenever I execute the program using eclipse, I am getting first thread run only (A, B) not second thread, bz java main method will take only one thread which will execute only first one not second one.

So only first thread will execute., please let me know if any wrong on this.

Javed said: (Jan 6, 2014)  
I Try this program in eclipse the output is A B.

Dipanshu Bhatia said: (May 21, 2015)  
I tried this program on BlueJ and the output is AB CD.

Anyone please explain the working of threads in this program?

Dipanshu Bhatia said: (May 21, 2015)  
I tried this program on BlueJ and the output is AB CD.

Anyone please explain the working of threads in this program?

Praveen said: (Jun 1, 2015)  
Every time I am getting "AB" using eclipse. So please tell me how to run second thread.

Aqueenni said: (Sep 30, 2015)  
I am doing it using notepad. Output is AB CD.

Abdul said: (Aug 10, 2016)  
The output is A B.

But when added try catch block it generates same AC BD output every time. Using Netbeans 8.1 on win 10.

Rksh25 said: (Nov 20, 2019)  
sb1 lock obtain by thread 1 and prints A and B. Just after this if T2 starts will get AB CD. In between the execution of T1, if T2 also starts it won't get lock so no output from T2 run method. We are not using wait n notify here for communication between t1 and t2.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.