wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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: 4343
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please suggest ways to cause deadlock in this code
 
Similar Threads
Question about the join method in Thread
Any Solution ?
Thread Deadlocking doubt
deadlock?
Thread Code has me confused