

Threads - Programming
Q1: 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();
}
}
A ABBCAD
B ABCBCAD
C CDADACB
D Output determined by the underlying platform.
ANS:A - ABBCAD 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. |


For help Students Orientation
Mcqs Questions
One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.