File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Please suggest ways to cause deadlock in this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Please suggest ways to cause deadlock in this code" Watch "Please suggest ways to cause deadlock in this code" New topic
Author

Please suggest ways to cause deadlock in this code

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Here is working code. Please suggest ways to ensure deadlock or increase the probability of deadlocking :




SCJP 6. Learning more now.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

You want a deadlock?

Easiest way to get a deadlock is to have the two threads require a lock on two separate objects, but for them to acquire them in different orders. For example:


In your example, it appears that the first thread is always synchronizing on h, and the second on h2. They share the same static int, so you may well get confusing behaviour, but I don't see how a deadlock can occur. You need both threads to be waiting for each other.
 
 
subject: Please suggest ways to cause deadlock in this code
 
Similar Threads
Any Solution ?
Thread Deadlocking doubt
deadlock?
Thread Code has me confused
Question about the join method in Thread