|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.openide.util.Mutex
Read-many/write-one lock. Allows control over resources that can be read by several readers at once but only written by one writer.
It is guaranteed that if you are a writer you can also enter the mutex as a reader. Conversely, if you are the only reader you are allowed to enter the mutex as a writer.
If the mutex is used only by one thread, the thread can repeatedly enter it as a writer or reader. So one thread can never deadlock itself, whichever order operations are performed in.
There is no strategy to prevent starvation. Even if there is a writer waiting to enter, another reader might enter the section instead.
Examples of use:
Mutex m = new Mutex ();
// Grant write access, compute an integer and return it:
return (Integer)m.writeAccess (new Mutex.Action () {
public Object run () {
return new Integer (1);
}
});
// Obtain read access, do some computation, possibly throw an IOException:
try {
m.readAccess (new Mutex.ExceptionAction () {
public Object run () throws IOException {
if (...) throw new IOException ();
return null;
}
});
} catch (MutexException ex) {
throw (IOException)ex.getException ();
}
Inner Class Summary | |
static interface |
Mutex.Action
Action to be executed in a mutex without throwing any checked exceptions. |
static interface |
Mutex.ExceptionAction
Action to be executed in a mutex, possibly throwing checked exceptions. |
static class |
Mutex.Privileged
Provides access to Mutex's internal methods. |
Field Summary | |
static Mutex |
EVENT
Mutex that allows code to be synchronized with the AWT event dispatch thread. |
Constructor Summary | |
Mutex()
Default constructor. |
|
Mutex(Mutex.Privileged privileged)
|
|
Mutex(Object lock)
Enhanced constructor that permits specifying an object to use as a lock. |
Method Summary | |
void |
postReadRequest(Runnable run)
Posts a read request. |
void |
postWriteRequest(Runnable run)
Posts a write request. |
Object |
readAccess(Mutex.Action action)
Run an action only with read access. |
Object |
readAccess(Mutex.ExceptionAction action)
Run an action with read access and possibly throw a checked exception. |
void |
readAccess(Runnable action)
Run an action with read access, returning no result. |
String |
toString()
toString |
Object |
writeAccess(Mutex.Action action)
Run an action with write access. |
Object |
writeAccess(Mutex.ExceptionAction action)
Run an action with write access and possibly throw an exception. |
void |
writeAccess(Runnable action)
Run an action with write access and return no result. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final Mutex EVENT
Constructor Detail |
public Mutex(Object lock)
readAccess(org.openide.util.Mutex.Action)
and during the
whole execution of writeAccess(org.openide.util.Mutex.Action)
. The ability to specify locks
allows several Mutex
es to synchronize on one object or to synchronize
a mutex with another critical section.lock
- lock to usepublic Mutex()
public Mutex(Mutex.Privileged privileged)
privileged
- can enter privileged states of this Mutex
This helps avoid creating of custom Runnables.Method Detail |
public Object readAccess(Mutex.Action action)
action
- the action to performMutex.Action.run()
public Object readAccess(Mutex.ExceptionAction action) throws MutexException
MutexException
and thrown from this method. One is encouraged
to catch MutexException
, obtain the inner exception, and rethrow it.
Here is an example:
Note that runtime exceptions are always passed through, and neither
require this invocation style, nor are encapsulated.
try {
mutex.readAccess (new ExceptionAction () {
public void run () throws IOException {
throw new IOException ();
}
});
} catch (MutexException ex) {
throw (IOException) ex.getException ();
}
action
- the action to executeMutex.ExceptionAction.run()
MutexException
- encapsulates a user exceptionRuntimeException
- if any runtime exception is thrown from the run methodreadAccess(Mutex.Action)
public void readAccess(Runnable action)
action
- the action to performreadAccess(Mutex.Action)
public Object writeAccess(Mutex.Action action)
action
- the action to performMutex.Action.run()
public Object writeAccess(Mutex.ExceptionAction action) throws MutexException
try {
mutex.writeAccess (new ExceptionAction () {
public void run () throws IOException {
throw new IOException ();
}
});
} catch (MutexException ex) {
throw (IOException) ex.getException ();
}
action
- the action to executeMutex.ExceptionAction.run()
MutexException
- an encapsulated checked exception, if anyRuntimeException
- if a runtime exception is thrown in the actionwriteAccess(Mutex.Action)
,
readAccess(Mutex.ExceptionAction)
public void writeAccess(Runnable action)
action
- the action to performwriteAccess(Mutex.Action)
,
readAccess(Runnable)
public void postReadRequest(Runnable run)
run
- runnable to runpublic void postWriteRequest(Runnable run)
run
- runnable to runpublic String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |