File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 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: 433
Thanks. I add the loop as you suggested. Now it hangs there.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I'm confused with the deadlock exampl in Java Tutorial.