This interface represents a flexible API for preventing thread concurrency with locking. Lock defines four methods for acquiring a lock. The simplest method is lock( ) which blocks indefinitely and uninterruptibly until the lock is acquired . This method is similar to entering a synchronized block. lockInterruptibly( ) blocks until the lock is acquired or until the thread is interrupted . The no-argument version of tryLock( ) acquires the lock and returns TRue if the lock is currently available or returns false without blocking if the lock is unavailable. The two-argument version of tryLock( ) is a timed method: it blocks until it acquires the lock (in which case it returns true ), or until the specified timeout elapses (in which case it returns false ), or until the thread is interrupted (in which case it throws InterruptedException ). Once a Lock has been acquired, no other thread can acquire it until it is released with the unlock( ) method. In order to ensure that locks are always released, even in the presence of unanticipated exceptions, it is typical to begin a try block immediately after acquiring the lock and to call unlock( ) from the associated finally clause. Obtain a Condition object associated with a Lock by calling newCondition( ) . See Condition for details. See ReentrantLock for a concrete implementation of the Lock interface. public interface Lock { // Public Instance Methods void lock ( ); void lockInterruptibly ( ) throws InterruptedException; Condition newCondition ( ); boolean tryLock ( ); boolean tryLock (long time , java.util.concurrent.TimeUnit unit ) throws InterruptedException; void unlock ( ); } ImplementationsReentrantLock , ReentrantReadWriteLock.ReadLock , ReentrantReadWriteLock.WriteLock Returned ByReadWriteLock.{readLock( ) , writeLock( )} , ReentrantReadWriteLock.{readLock( ) , writeLock( )} |