public class Syntest{ public static void main(String []args){ final StringBuffer s1=new StringBuffer(); final StringBuffer s2=new StringBuffer(); new Thread(){ public void run(){ synchronized(s1){ s1.append("a"); synchronized("b"){ System.out.println(s1); System.out.println(s2); } } } }.start(); new Thread(){ public void run(){ synchronized(s2){ s2.append("c"); synchronized(s1){ s1.append("d"); System.out.println(s2); System.out.println(s1); } } } } .start(); } } a)print ABBCAD b)print CDDACB c)print ADCBADBC d)The output is a not-deterministic point because of a possible deadlock condition e)The output is dependent on the threading model of the ysstem the program is running on. What could be the output of this question?.I go with D,E,correct me if i am wrong.
Stevie Kaligis
Ranch Hand
Joined: Feb 04, 2001
Posts: 400
posted
0
answer D & E : Agree...! D. waiting for each other's lock ! E. possible if one of those thread run and finish before other thread has a change to run. stevie
Ravindra Mohan
Ranch Hand
Joined: Mar 16, 2001
Posts: 216
posted
0
Well in my opinion the correct answer is "a cad". Not because my compiler tells me that!! But because , there is no <code>sleep()</code> been called so there is no question of platform dependency arising here as which Thread will <code> notify()</code> whom and such issues. The querry is a straight forward implementation of two anonymous Threads created inside the main method and running by a call to <code>start()</code> method. Thus we have three thread in all two anonymous Thread and the main thread itself. The synchronised block is locking the monitor for the currently excuting thread. But since there is no call to sleep or notify. Hope I am right. Let me know if I am wrong. Ravindra Mohan.
[This message has been edited by Ravindra Mohan (edited May 09, 2001).]
nitin sharma
Ranch Hand
Joined: Feb 24, 2001
Posts: 290
posted
0
hi, I think lawson ans steven are right.
James Du
Ranch Hand
Joined: Mar 23, 2001
Posts: 186
posted
0
I think only e is right: The output is dependent on the threading model of the system the program is running on. First, you can't expect any ganranteed string output because the order of the execution of the 2 annoymous threads are not guaranteed. Second, no deadlock issue here, the first thread never intend to claim the lock of S2, pls. look at the code carefully. Correct me if i'm wrong. Regards, James