ReentrantLock
An explicit lock from java.util.concurrent.locks that a thread may acquire more than once.
In more detail
Call lock() before a try block and unlock() in its finally block. Each lock() adds to a hold count, and the lock is free only when unlock() has been called as often. tryLock() returns false at once instead of waiting, and unlocking a lock you do not hold throws IllegalMonitorStateException.
Example
lock.lock();
try { count++; } finally { lock.unlock(); }Related terms
- synchronized — The Java keyword that lets only one thread at a time run a block of code guarded by the same lock object.
- race condition — A bug whose outcome depends on the timing of two things happening at once.
Taught in
- Platform And Virtual Threads, Runnable And Callable — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Thread Safety: Locks And Atomics — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep