This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes I'm confused with the deadlock exampl in Java Tutorial. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "I Watch "I New topic
Author

I'm confused with the deadlock exampl in Java Tutorial.

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433

In my machine, the result is:
Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed back to me!
Gaston: Alphonse has bowed to me!
Alphonse: Gaston has bowed back to me!

Why doesn't the code hang there? If the result is OK,what does the deadlock mean?


To be or not to be. It's a question.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Run this enough times, and it should indeed hang. Try putting a loop in main() to run this 500 times. It's a timing issue; whether or not it hangs depends on how quickly the second thread can be started.

I just compiled and ran your program and it hung the first time!


[Jess in Action][AskingGoodQuestions]
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
Thanks. I add the loop as you suggested. Now it hangs there.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: I'm confused with the deadlock exampl in Java Tutorial.