• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please suggest ways to cause deadlock in this code

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is working code. Please suggest ways to ensure deadlock or increase the probability of deadlocking :



 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic