Known Direct Subclasses
ConditionLock |
ConditionLock is a Lock with a built in state variable. |
|
Class Overview
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();
}
}
}
Summary
Public Constructors |
|
Lock()
Create a lock, which is initially not locked.
|
Public Methods |
synchronized
final
void
|
lock()
Acquire the lock.
|
synchronized
final
void
|
unlock()
Release the lock.
|
[Expand]
Inherited Methods |
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this object.
|
boolean
|
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
void
|
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
final
Class<?>
|
getClass()
Returns the runtime class of this Object .
|
int
|
hashCode()
Returns a hash code value for the object.
|
final
void
|
notify()
Wakes up a single thread that is waiting on this object's
monitor.
|
final
void
|
notifyAll()
Wakes up all threads that are waiting on this object's monitor.
|
String
|
toString()
Returns a string representation of the object.
|
final
void
|
wait()
Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object.
|
final
void
|
wait(long timeout, int nanos)
Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object, or
some other thread interrupts the current thread, or a certain
amount of real time has elapsed.
|
final
void
|
wait(long timeout)
Causes the current thread to wait until either another thread invokes the
notify() method or the
notifyAll() method for this object, or a
specified amount of time has elapsed.
|
|
Public Constructors
public
Lock
()
Create a lock, which is initially not locked.
Public Methods
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.
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.