| java.lang.Object threaddemo.locking.PrivilegedLock
PrivilegedLock | final public class PrivilegedLock (Code) | | Ability to acquire or release a lock by itself.
This class can be used when one wants to avoid creating a
bunch of Runnables. Instead use:
p.enter*();
try {
// your code here
} finally {
p.exit*();
}
You must be careful to match enter and exit calls reliably and exactly.
Please read the Javadoc for each method carefully.
You must control the related Lock, i.e. you must be the creator of
the Lock. Thus you may create a PrivilegedLock for efficient access within
a package and only expose the lock to outside code, to ensure that it is
not abused by being entered and not exited.
See Also: Lock |
Constructor Summary | |
public | PrivilegedLock() Create a new privileged key to a lock. |
Method Summary | |
public void | enterRead() Enter read access for this lock.
You must ensure that
PrivilegedLock.exitRead is reliably called
when you are done. The normal way to do this is as follows:
p.enterRead();
// must be no additional code here!
try {
// whatever code...
} finally {
// must be no additional code here!
p.exitRead();
}
Detailed behavior:
- You may already be holding the read or write lock.
| public void | enterWrite() Enter write access for this lock.
You must ensure that
PrivilegedLock.exitWrite is reliably called
when you are done. The normal way to do this is as follows:
p.enterWrite();
// must be no additional code here!
try {
// whatever code...
} finally {
// must be no additional code here!
p.exitWrite();
}
Detailed behavior:
- You may already be holding the write lock.
| public void | exitRead() Exit the read lock.
For important usage instructions, see
PrivilegedLock.enterRead .
Detailed behavior:
- You must have already entered this lock in read mode (once for
every time you exit it).
- You must exit a lock in the same thread you entered it.
- If this lock has a level, it must be the last lock with a level
which you entered in this thread.
| public void | exitWrite() Exit the write lock.
For important usage instructions, see
PrivilegedLock.enterWrite .
Detailed behavior:
- You must have already entered this lock in write mode (once for
every time you exit it).
- You must exit a lock in the same thread you entered it.
- If this lock has a level, it must be the last lock with a level
which you entered in this thread.
| public RWLock | getLock() Get the associated lock. | final synchronized void | setParent(DuplexLock parent) |
PrivilegedLock | public PrivilegedLock()(Code) | | Create a new privileged key to a lock.
(It may only be used in one lock.)
|
enterRead | public void enterRead()(Code) | | Enter read access for this lock.
You must ensure that
PrivilegedLock.exitRead is reliably called
when you are done. The normal way to do this is as follows:
p.enterRead();
// must be no additional code here!
try {
// whatever code...
} finally {
// must be no additional code here!
p.exitRead();
}
Detailed behavior:
- You may already be holding the read or write lock. But you must
still nest entries and exits, 1-to-1.
- If this lock has a level, you may not enter it if you are already
holding another lock with a smaller or equal level in this thread.
- If another thread is holding the write lock, this method
will block until it leaves.
|
enterWrite | public void enterWrite()(Code) | | Enter write access for this lock.
You must ensure that
PrivilegedLock.exitWrite is reliably called
when you are done. The normal way to do this is as follows:
p.enterWrite();
// must be no additional code here!
try {
// whatever code...
} finally {
// must be no additional code here!
p.exitWrite();
}
Detailed behavior:
- You may already be holding the write lock. But you must
still nest entries and exits, 1-to-1.
- You may not be holding the read lock - even if inside
the write lock.
- If this lock has a level, you may not enter it if you are already
holding another lock with a smaller or equal level in this thread.
- If other threads are holding the read or write lock, this method
will block until they all leave.
|
exitRead | public void exitRead()(Code) | | Exit the read lock.
For important usage instructions, see
PrivilegedLock.enterRead .
Detailed behavior:
- You must have already entered this lock in read mode (once for
every time you exit it).
- You must exit a lock in the same thread you entered it.
- If this lock has a level, it must be the last lock with a level
which you entered in this thread. You cannot interleave exits of
locks with levels; they must nest.
- If this read access is inside another read access, this method
will return immediately.
- If this read access is the outermost read access, and not inside any
write access, it will return immediately.
- If this read access is the outermost read access within a write access,
it will return immediately.
|
exitWrite | public void exitWrite()(Code) | | Exit the write lock.
For important usage instructions, see
PrivilegedLock.enterWrite .
Detailed behavior:
- You must have already entered this lock in write mode (once for
every time you exit it).
- You must exit a lock in the same thread you entered it.
- If this lock has a level, it must be the last lock with a level
which you entered in this thread. You cannot interleave exits of
locks with levels; they must nest.
- If this write access is inside another write access, this method
will return immediately.
- If this write access is the outermost write access, it will return
immediately.
|
getLock | public RWLock getLock()(Code) | | Get the associated lock.
You must have already created a lock with this privileged handle.
the lock associated with this object |
|
|