• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to construct a minimalistic deadlock  RSS feed

 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to construct a deadlock deliberately by coding this scenario:

1) Thread A enters a synchronized method of object foo
2) Thread A goes to sleep (thus still holding the key of foo)
3) Thread B enters a synchronized method of object bar
4) Thread B tries to enter a synchronized method of foo (, but cant)
5) Thread A wakes up and tries to enter a synchronized method of bar => deadlock

I'm trying to keep the code really minimalistic, i.e. just 2 threads and 2 objects.
I also dont want to use this syntax:



but would like to achieve this just by calling synchronized methods of 2 different objects (as stated above).


I've been fumbling around for quite some time but I'm giving up now...

Help anyone?
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bora Sabrioglu wrote:
I've been fumbling around for quite some time but I'm giving up now...


Can you show us the results of your fumbling around? And what issues are you running into?

Henry
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I finally got it:



The 'this' reference was the missing piece of the puzzle...
I couldn't produce a deadlock, but now I have one
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm. I inserted some s.o.p's and there continues to be some output:



I wonder if this is a deadlock or not... I guess not yet...
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bora Sabrioglu wrote:
I wonder if this is a deadlock or not... I guess not yet...


And your guess is correct. Your two threads are using different locks, so they can't deadlock on each other. Try...

1. Having the two threads share the same two locks.
2. Have one thread grab one lock first, and have the other thread grab the other lock first.

Henry

PS... I cleared the topic resolve flag for you -- so this topic is marked as not resolved.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.

I think it works now. I extracted the A and B objects out of run and made them a member, so only one copy of them exists... and also influenced the order of object/lock access:



The output stops now.
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It may also be a good idea to add the sleep() to class B (just like class A). Depending on the scheduler, it is possible for the t2 thread to run first. This thread can acquire the B lock, and then immediately acquire the A lock. In which case ...

The t1 thread will be blocked for the A lock. The t2 thread will recursively keep acquiring both locks, until it runs out of memory, throws an exception, releases all the locks. And once all the locks are released, then t1 will do the same.

Henry
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're right... haven't thought about that... thanks.
 
Ivan Korhner
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find this useful: http://korhner.github.io/java/multithreading/detect-java-deadlocks-programmatically/

It's a simple way to detect Java deadlocks programmatically so instead of relying to debugger or lack of output, you can just output deadlocked threads with stack trace when a deadlock happens.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!