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.
Kannan said:
1 decade ago
I excuted this program output : ABBCAD
But you told - "Output determined by the underlying platform". Please explain.
But you told - "Output determined by the underlying platform". Please explain.
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.
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.
I think the author is confusing synchronization with order of execution.
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. ---
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:
1 decade ago
I excuted this program output : ABBCAD
But you told - "Output determined by the underlying platform". Please explain.
But you told - "Output determined by the underlying platform". Please explain.
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.
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'.
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
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.
Reha said:
1 decade ago
A
B
BC
AD
I have also got this output.
B
BC
AD
I have also got this output.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers