Explicit locking

The synchronized keyword is an intrinsic locking mechanism. It is pretty convenient, there are a couple of limitations as well. We cannot interrupt a thread waiting for an intrinsic lock, for example. There is also no way to time out the wait while acquiring the lock.

There are use cases where these capabilities are needed; at such times, we use explicit locking. The Lock interface allows us to overcome these limitations:

ReentrantLock duplicates the functionality of the synchronized keyword. A thread already holding it can acquire it again, just like with synchronized semantics. Memory visibility and mutual exclusion guarantees ...

Get Concurrent Patterns and Best Practices now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.