• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Threads

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I think lawson ans steven are right.
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
permaculture is giving a gift to your future self. After reading this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic