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 2 of 2.

Rahul said:   1 decade ago
If the data members are declared using final keyword how can one modify the stringBuffer object could somebody please explain.

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

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'.

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.

Madhu Sudhan Reddy said:   1 decade ago
I excuted this program output : ABBCAD

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

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. ---

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.

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.


Post your comments here:

Your comments will be displayed after verification.