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.
-
2can you please provide the code. it would be much easier for someone to answer you question if the related code was provided – pulasthi May 14 '13 at 15:16
-
Post some code for a definitive answer. In general, I don't think so, but you can always run it with Jmeter under load and see what breaks. – Taylor May 14 '13 at 15:38
-
I have few DAO methods to call video server API, something like this: `@Override public Iterable extends BaseVideoServerRecordingChannel> getRecordingChannels() throws VideoServerException { synchronized (GlobalSync.SINGLE_SYNC_POINT) { List
channels = new ArrayList – MartinC May 15 '13 at 06:55(); //calling video server api return channels; }` There are two main threads calling those methods, so without a synchronization, time to time, request/response failed. I don't call methods from each other. -
I guess dead lock could occur only when something inside of these DAO methods would end up in infinite loop, but I don't seem to have these type of cycles here. – MartinC May 15 '13 at 07:00
-
You might wanat to check for livelocks as well. – John Kane May 15 '13 at 14:08
4 Answers
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.

- 40,646
- 13
- 77
- 103
-
One situation is: 1. Thread A requests the lock and askes Thread B to process samething 1. Thread A only releases the lock when Thread B is done. 3. Thread B needs the same lock to perform its tasks. Thats it. DeadLock!!! – Jose Renato May 14 '13 at 16:43
-
@JoseRenato It is not clear how *Thread A can ask Thread B to process something*... – assylias May 14 '13 at 16:45
-
3@assylias On way is: **Thread B** has a list of `Runnable` and, in **Thread B** `run()` method, at some point, this list is processed. So, now, if **Thread A** put some `Runnable` on **Thread B** list, then **Thread B** will be processing things asked from **Thread A**. Got it? – Jose Renato May 14 '13 at 16:56
-
@JoseRenato I had never thought of that - very nice. I have posted an example implementation. – assylias May 14 '13 at 17:11
-
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.

- 9,562
- 2
- 32
- 29
-
1thanks, I agree with you. I don't use kind of possibly infinite loops inside of sync blocks, so I guess I'll be fine. – MartinC May 15 '13 at 07:17
-
-
This is not a deadlock. Deadlock is when two or more threads/processes waiting for each others to finish in a cyclic order. If you have an infinite loop you have a problem in single threaded application as well. – amit May 16 '13 at 14:32
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.

- 10,612
- 11
- 61
- 60
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.

- 4,383
- 1
- 24
- 42