This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 4240
    
    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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please suggest ways to cause deadlock in this code
 
Similar Threads
Any Solution ?
Thread Code has me confused
Question about the join method in Thread
deadlock?
Thread Deadlocking doubt