aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Deadlocks in Threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Deadlocks in Threads?" Watch "Deadlocks in Threads?" New topic
Author

Deadlocks in Threads?

B Ani
Greenhorn

Joined: Feb 01, 2001
Posts: 3
Can anybody explain this code corresponding to deadlocks in Threads?
class syn1{
String msg;
syn1(String s){
msg = s;
}
synchronized void set(String t, syn1 a){
msg += "["+t;
a.get();//lock on other object.
try{
Thread.sleep(100);
}catch(Exception e){
System.out.println("Exception");
}
msg+="]";
}
synchronized String get(){
try{
Thread.sleep(100);
}catch(Exception e){
System.out.println("Exception");
}
return msg;
}
}
class Th1 implements Runnable{
syn1 s1;
syn1 s2;
Th1(syn1 s1, syn1 s2){
this.s1 = s1;
this.s2 = s2;
}
public void run(){
while(true){
s2.set((Thread.currentThread()).getName(), s1);
System.out.println(s2.get());
s1.set((Thread.currentThread()).getName(), s2);
System.out.println(s1.get());
}
}
}
class Dead{
public static void main(String args[]){
syn1 s1 = new syn1("s1 ");
syn1 s2 = new syn1("s2 ");
new Thread(new Th1(s1, s2), "T1").start();
new Thread(new Th1(s1, s2), "T2").start();//(1)
//comment the above line(1) and uncomment
//the Line(2) below to cause deadlock.
//new Thread(new Th1(s2, s1), "T2").start();//(2)
}
}
Priya Kannan
Ranch Hand

Joined: Jan 19, 2001
Posts: 72
Hi Ani,
Let me give it a try.First of all, I don't think this code is an example for a deadlock condition!My understanding of a dead lock is a situation where a process cannot proceed any further, as a result of which the system hangs.As far as this code is concerned,it works quite clean for both the conditions.
In the first case,since both the threads T1 & T2 synchronise on set() & get() methods via s1 & s2(can I say so?) in the same order,they manage to execute seemingly simultaneously(Actually thread T2 gets active during the time T1 sleeps in the set() & get() method).And hence the output
G:\gudiya\Trials>java Dead
s2 [T1][T2]
s2 [T1][T2]
s1 [T1][T2]
s1 [T1][T2]
s2 [T1][T2][T1][T2]
s2 [T1][T2][T1][T2]
s1 [T1][T2][T1][T2]
s1 [T1][T2][T1][T2]
s2 [T1][T2][T1][T2][T1][T2]
s2 [T1][T2][T1][T2][T1][T2]
s1 [T1][T2][T1][T2][T1][T2]
s1 [T1][T2][T1][T2][T1][T2]
In the later case,T1's S2 synchronizes on set() and T1's S1 synchronizes on get().During the time T1 sleeps, T2 becomes active and wants to synchronize S1 on set().Now since,T1's S2 has already got the lock on set(),T2 waits till S2's lock on set() is released.
s2.set((Thread.currentThread()).getName(), s1);
After this statement in run(),S2's lock on set() is released and the same locks get().So, now the first S2 [T1] gets printed.What comes next is the two threads alternating locks on set() & get() and printing the updated,appended version of their msg.
G:\gudiya\Trials>java Dead (under deadlock???)
s2 [T1]
s1 [T2][T1]
s1 [T2][T1]
s2 [T1][T2][T1]
s2 [T1][T2][T1]
s1 [T2][T1][T2][T1]
s1 [T2][T1][T2][T1]
s2 [T1][T2][T1][T2][T1]
s2 [T1][T2][T1][T2][T1]
s1 [T2][T1][T2][T1][T2][T1]
s1 [T2][T1][T2][T1][T2][T1]
s2 [T1][T2][T1][T2][T1][T2][T1]
s2 [T1][T2][T1][T2][T1][T2][T1]
I know the output isn't a very pretty sight.But remember, this is just a sample!!!
If you wanna get the feel of a real deadlock condition refer Shailendra Guggali's post
" Threads and Deadlock - a question in SCJP exam" posted February 04, 2001 12:07 AM
Disclaimer
*********
The output MAY not be the same on your machine!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deadlocks in Threads?