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.

Adam said:   7 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.

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

Shakeer Hussain said:   8 years ago
I got the output like:

A
B
BC
AD

Jamil said:   9 years ago
If we use multi-thread with a Synchronization block it will produce a same result.

Soumya Ghosh said:   9 years 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.

Yuga said:   9 years ago
Hi,

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

Please help to understand.

Thank you.

Kishan said:   10 years 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.

Akshay said:   10 years ago
@Rahul.

I have the same exact question.

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

Reha said:   10 years ago
A
B
BC
AD

I have also got this output.

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


Post your comments here:

Your comments will be displayed after verification.