I don't understand the difference between them. I thought a lock from the lock interface was reentrant too then what's the difference between them? When would you use each?
-
2(i) Lock is not necessarily reentrant (read the javadoc) (ii) What is the difference between List and ArrayList? – assylias Apr 22 '16 at 16:57
1 Answers
Lock
is an interface. It defines a set of methods that all locks should have.
ReentrantLock
is a concrete class that implements the Lock
interface. It implements all the methods defined in Lock
, plus much more. Additionally, as mentioned in the name, the lock is re-entrant which means the same thread can acquire the lock as many times as it wants. This is essentially the same behavior as the native object monitor locks provided by the synchronized
keyword.
The Lock
interface makes it possible for you to implement your own kind of lock. For example, you might design a lock that issues an HTTP request (performing network I/O) to lock a remote resource. Another class that uses your lock wouldn't care about the internal details of your lock; it only cares that your custom lock respects the Lock
interface.

- 17,911
- 6
- 53
- 80