I came across this line which states that "However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access"in the overall description of Java class [ConcurrentHashMap] (https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html). My question is: does this mean that the ConcurrentHasMap does not prevent dead lock? Also I thought being thread-safe imply no dead lock will take place?
-
1Operations on ConcurrentHashMap does not cause a deadlock - and hopefully noone would label a piece of code as thread safe if it could cause a deadlock. – nos Oct 24 '16 at 13:49
-
Thanks for the quick feedback and accept! – GhostCat Oct 24 '16 at 14:14
-
@nos: it’s not so easy. In Java 8, `ConcurrentHashMap` offers the `compute…` methods that allow to update a mapping while locking that specific key (or bucket), which implies that the provided function *must not* try to update another mapping during the evaluation (or try to acquire any other lock). So *correctly used*, a `ConcurrentHashMap` will never cause a deadlock, but the possibility of a deadlock when being used incorrectly doesn’t imply that `ConcurrentHashMap` is not thread safe… – Holger Oct 24 '16 at 17:11
-
interesting post: http://stackoverflow.com/questions/3292577/is-it-possible-for-concurrenthashmap-to-deadlock – Ravindra babu Oct 24 '16 at 18:00
2 Answers
You get things the wrong way: whenever you create a design that requires locking, you open up the possibility for dead locks.
That doesn't necessarily mean that any such architecture is per se vulnerable to dead-locks.
Example: a typical dead lock situation is when thread A has lock L1 and waits for lock L2; whereas thread B holds L2 and needs L1. If you only have one lock object, then that scenario is one .. that can't happen.
In other words: you are not using class X it would prevent deadlocks. That is not possible. If at all, you might be using class X because it offers you functionality that allows you to come up with a "guaranteed-dead-lock-free" design!

- 137,827
- 25
- 176
- 248
A deadlock can occur only when there are two different locks, i.e. when you are holding a lock and waiting for another lock to release. (There are more conditions on deadlocks, however).
As the ConcurrentHashMap tries to avoid locks where possible, you are not able to acquire a lock with operations only on the map that the map may wait for. Hence, operations only on the map do not cause deadlocks.
However, thread-safety does not mean deadlock free. It only guarantees that the code will operate according to its interface, even when called from multiple threads. Making a class thread-safe usually includes adding locks to guarantee safe execution.
You may also want to have a look at the Wikipedia article.

- 4,463
- 3
- 28
- 36
-
Nice answer too; and actually not so much different from mine. Lets honor that. – GhostCat Oct 24 '16 at 14:15