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();
}
}
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:
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.
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:
9 years ago
I got the output like:
A
B
BC
AD
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:
10 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.
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:
1 decade 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.
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:
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.
I hope you understand.
Akshay said:
1 decade ago
@Rahul.
I have the same exact question.
How can you modify the stringbuffer if it is declared with the "final" keyword?
I have the same exact question.
How can you modify the stringbuffer if it is declared with the "final" keyword?
Reha said:
1 decade ago
A
B
BC
AD
I have also got this output.
B
BC
AD
I have also got this output.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers