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 Is it deadlock? 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 "Is it deadlock?" Watch "Is it deadlock?" New topic
Author

Is it deadlock?

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Hello



In the above code, the synchronized method a(), when executed, obtains a lock on it's own object. It then calls synchronized method b() which also needs to acquire a lock on it's own object. If Java did not allow a thread to reacquire it's own lock method b() would be unable to proceed until method a() completed and released the lock; and method a() would be unable to complete until method b() completed. Result: deadlock


Is it deadlock?
[ October 30, 2007: Message edited by: Jim Yingst ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Did you try running it? If the methods complete and the JVM exits, that's a clue that it's not deadlock.

Java synchronization is reentrant. Deadlock will only occur when you're talking about locks on two or more different monitor objects.

I edited your post to move the discussion out of the code block - the lines were too long, and messed up the formatting of the page. (Inside code blocks, line breaks will not be inserted automatically.)


"I'm not back." - Bill Harding, Twister
 
wood burning stoves
 
subject: Is it deadlock?
 
Similar Threads
Deadlock
nested synchronized statements
Synchronised methods
Regarding Join method in Threads
Deadlock