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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Deadlock explaination please" Watch "Deadlock explaination please" New topic
Author

Deadlock explaination please

Alex Fong
Greenhorn

Joined: May 15, 2007
Posts: 2
can anyone explain to me this piece of code and how the deadlock occured? thanks

[ June 07, 2007: Message edited by: Barry Gaunt ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Alex,

Welcome to JavaRanch!

#1: It is bad practice to do like:
try{
Thread.sleep(500);
}catch (Exception e){}
}
How will you come to know what happened of exception if thrown!

#2: Root of deadlock:
DeadLock t1 = new DeadLock(obj1, obj2);
DeadLock t2 = new DeadLock(obj2, obj1);

The order or resources, the thread will work with:
->t1 will first acquire the lock of resourceA(obj1) and then go to grab the lock for resourceB(obj2).
->t1 will first acquire the lock of resourceA(obj2) and then go to grab the lock for resourceB(obj1).

Meanwhile if both the threads are having lock on one-one object, then they
will wait for the other object that can't be released. Waiting for each
other.



Thanks,
[ June 07, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please, in future, enclose your properly indented code within code tags ().


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deadlock explaination please
 
Similar Threads
Wait And Notify concept
Deadlock K&B program
Basic thread doubt
deadlock?
synchronized threads