O'Reilly logo

Concurrent Patterns and Best Practices by Atul S. Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required