Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
What are the types of locks available in java.util.concurrent.locks?
The java.util.concurrent.locks package offers explicit lock implementations that go beyond what the intrinsic synchronized keyword can do.
| Lock type | Best used for |
| ReentrantLock | General-purpose mutual exclusion with tryLock, timeouts, interruptibility, and fairness options. |
| ReentrantReadWriteLock | Read-heavy data where many readers can proceed concurrently but writers need exclusive access. |
| StampedLock | High-throughput read-mostly access using an optimistic read mode that avoids locking entirely when uncontended. |
All of them implement the Lock interface (except StampedLock, which has its own stamp-based API) and must be released manually, typically in a finally block, since there's no automatic release the way a synchronized block provides.
More Related questions...