Class java.util.Lock
All Packages This Package Previous Next
Class java.util.Lock
java.lang.Object
|
+----java.util.Lock
-
public class
Lock
-
extends Object
The Lock class provides a simple, useful interface to a lock.
Unlike monitors which synchronize access to an object, locks
synchronize access to an arbitrary set of resources (objects,
methods, variables, etc.).
The programmer using locks must be responsible for clearly defining
the semantics of their use and should handle deadlock avoidance in
the face of exceptions.
For example, if you want to protect a set of method invocations with
a lock, and one of the methods may throw an exception, you must be
prepared to release the lock similarly to the following example:
class SomeClass {
Lock myLock = new Lock();
void someMethod() {
myLock.lock();
try {
StartOperation();
ContinueOperation();
EndOperation();
} finally {
myLock.unlock();
}
}
}
-
Version:
-
1.7, 31 Jan 1995
-
Author:
-
Peter King
-
Lock()
-
Create a lock, which is initially not locked.
-
lock()
-
Acquire the lock.
-
unlock()
-
Release the lock.
Lock
public Lock()
-
Create a lock, which is initially not locked.
lock
public final synchronized void lock()
-
Acquire the lock. If someone else has the lock, wait until it
has been freed, and then try to acquire it again. This method
will not return until the lock has been acquired.
unlock
public final synchronized void unlock()
-
Release the lock. If someone else is waiting for the lock, the
will be notitified so they can try to acquire the lock again.
All Packages This Package Previous Next