|
|
|
|
To quote from the javadoc pages for interface java.util.concurrent.locks.Lock : A lock is a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: only one thread at a time can acquire the lock and all access to the shared resource requires that the lock be acquired first. However, some locks may allow concurrent access to a shared resource, such as the read lock of aReadWriteLock.
It goes on to describe how to use such locks: In most cases, the following idiom should be used:
This construct guarantees that only one thread at a time can enter the critical
section -- the section of code between the call to lock() and the call
to unlock().
Note: It is critically important that the unlock() operation be placed in a finally block. If the code within the critical section were to throw an exception, the lock should still be unlocked. Without this correct placement of unlock(), other threads might be permanently blocked. |
| The page was last updated February 19, 2008 |