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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.