Java Programming - Threads - Discussion

Discussion Forum : Threads - Finding the output (Q.No. 11)
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(); 
    } 
}
ABBCAD
ABCBCAD
CDADACB
Output determined by the underlying platform.
Answer: Option
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.

Discussion:
18 comments Page 1 of 2.

Sundar said:   1 decade ago
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. ---

Mayru Pabale said:   1 decade ago
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'.

Sasikanta said:   8 years ago
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.

Soumya Ghosh said:   1 decade ago
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.

Kishan said:   1 decade ago
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.

Samba said:   1 decade ago
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.

Kumar said:   1 decade ago
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

Adam said:   8 years ago
In order to get an ordered output, one should use "synchronized (h)" instead of "synchronized (this) ", because "this" points to the current thread, not to the object h.

BADRI said:   1 decade ago
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:   1 decade ago
start() always causes a thread to invoke the run() method.

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


Post your comments here:

Your comments will be displayed after verification.