Java Programming - Threads - Discussion

11. 

What will be the output of the program?

class Happy extends Thread 
{ 
    final StringBuffer sb1 = new StringBuffer(); 
    final StringBuffer sb2 = new StringBuffer(); 

    public static void main(String args[]) 
    { 
        final Happy h = new Happy(); 

        new Thread() 
        { 
            public void run() 
            { 
                synchronized(this) 
                { 
                    h.sb1.append("A"); 
                    h.sb2.append("B"); 
                    System.out.println(h.sb1); 
                    System.out.println(h.sb2); 
                } 
            } 
        }.start(); 

        new Thread() 
        { 
            public void run() 
            { 
                synchronized(this) 
                { 
                    h.sb1.append("D"); 
                    h.sb2.append("C"); 
                    System.out.println(h.sb2); 
                    System.out.println(h.sb1); 
                } 
            } 
        }.start(); 
    } 
}

[A]. ABBCAD
[B]. ABCBCAD
[C]. CDADACB
[D]. Output determined by the underlying platform.

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.


Kannan said: (Oct 29, 2010)  
I excuted this program output : ABBCAD

But you told - "Output determined by the underlying platform". Please explain.

Badri said: (Jul 12, 2011)  
I think you didn't run the programme multiple times.

Whenever you run the programme multiple times different outputs will come, check once again.

Corrie said: (Jul 27, 2011)  
start() always causes a thread to invoke the run() method.

I think the author is confusing synchronization with order of execution.

Sundar said: (Dec 15, 2011)  
The given answer is correct.

I have tested the above code in Windows.

D:\Java>javac Happy.java

D:\Siva\Java>java Happy
C
D
DA
CB

D:\Siva\Java>java Happy
C
D
DA
CB

D:\Siva\Java>java Happy
A
BC
BC
AD

D:\Siva\Java>java Happy
A
BC
BC
AD

------------------
I have tested the above code in Linux. The Output is

A
B
BC
AD

--- Therefore, Output determined by the underlying platform. ---

Madhu Sudhan Reddy said: (Mar 6, 2012)  
I excuted this program output : ABBCAD

But you told - "Output determined by the underlying platform". Please explain.

Samba said: (May 3, 2013)  
I executed this program multiple time. But it gives consistency results is: A B BC AD, In above code we use synchronized block, so single thread can execute buffered variables at a time.

Mayru Pabale said: (Jul 25, 2014)  
How is it dependent on 'underlying platform'? As far as I know platform doesn't play any role when it comes to Multithreading. Its thread scheduler that affects o/p in such cases. You should have said 'Different o/p for different run depending upon thread scheduler'.

Kumar said: (Jul 25, 2014)  
One way to determine the answer choice is - notice there are printLN in the code and no new lines in the output, so by elimination its D. But understand that should not be the rationale

Rahul said: (Aug 3, 2014)  
If the data members are declared using final keyword how can one modify the stringBuffer object could somebody please explain.

Reha said: (Oct 18, 2014)  
A
B
BC
AD

I have also got this output.

Akshay said: (Dec 6, 2014)  
@Rahul.

I have the same exact question.

How can you modify the stringbuffer if it is declared with the "final" keyword?

Kishan said: (Dec 24, 2014)  
It's doesn't modify the object, it modify the state of object. When we use outer var in inner class it's should be the final. Otherwise it's give compile time error.

I hope you understand.

Yuga said: (Feb 27, 2015)  
Hi,

If it is the case. Then, what is the use of synchronized block. Why it is needed?

Please help to understand.

Thank you.

Soumya Ghosh said: (Jun 23, 2015)  
Can synchronized block be regarded as a set of atomic instructions?

synchronized(this)
{
1. statement 1
2. statement 2
3. statement 3
}

If control enters inside sync() block will all the statements be executed.

Jamil said: (Dec 9, 2015)  
If we use multi-thread with a Synchronization block it will produce a same result.

Shakeer Hussain said: (May 11, 2016)  
I got the output like:

A
B
BC
AD

Sasikanta said: (Jul 30, 2017)  
Option D is correct.

Those who don't see the difference, please change the value and execute multiple times, the answer will vary. Yes, you have to try a lot because I got the variation 3 to 4 time after 40 to 50 times of execution.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.