Question

I have two running threads calling few methods (5 or 6) where I specified synchronized block inside and use only one object to lock it. Is there any chance of deadlock with having only one sync point? So far, I haven't seen such a situation, but still. Thanks for help.

Was it helpful?

Solution 2

If you are only using synchronized calls (i.e. no join, wait, notify, etc.) then the only way you can "deadlock" (using the term broadly here) is if one of the threads fails to exit the synchronised block (e.g. executes an infinite loop, doesn't return from a call to request some resource, etc.). So, yes, it's possible.

OTHER TIPS

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.

If your methods get called from each other there might be a situation. Find more detail with example in documentation.

If you just have one lock for locking shared resources deadlock cannot occur. You can check if the Coffman's conditions are met to identify potential deadlock.

Without seeing your code it is difficult to say. But as you have described it you are most likely fine. This link talks about ways of avoiding deadlock. For example take the following quote from the article, it says that one way to avoid deadlocks is to check for a "...nested synchronized block or calling one synchronized method from other or trying to get lock on different object".

Another thing you should be aware of is a live lock. This occurs when one threads action is in response to another threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top