This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android 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: 430

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: 24183
    
  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: 430
Thanks. I add the loop as you suggested. Now it hangs there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I'm confused with the deadlock exampl in Java Tutorial.
 
Similar Threads
Why first output is second output
Question on Synchronized mehods
[B]Class Monitor Vs. Object Monitor[/B]
Explanation for Dead Lock
Deadlock