| java.lang.Object | |
| ↳ | sun.misc.Lock |
Known Direct Subclasses
|
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();
}
}
}
| Public Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Create a lock, which is initially not locked.
| |||||||||||
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Acquire the lock.
| |||||||||||
Release the lock.
| |||||||||||
|
[Expand]
Inherited Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
Create a lock, which is initially not locked.
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.
| InterruptedException | if any thread has interrupted this thread. |
|---|
Release the lock. If someone else is waiting for the lock, the will be notitified so they can try to acquire the lock again.