|
WebObjects 5.2.3 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.webobjects.foundation.NSRecursiveLock
NSRecursiveLock defines a lock that may be acquired multiple times by the same thread without causing a deadlock (a situation where a thread is permanently blocked waiting for itself to relinquish a lock). While the locking thread has one or more locks, all other threads are prevented from accessing the code protected by the lock. Here's an example where a recursive lock functions properly but other lock types would deadlock:
NSRecursiveLock theLock = new NSRecursiveLock(); ... theLock.lock(); //lengthy operations involving global data theLock.lock(); //possibly invoked in a subroutine ... theLock.unlock(); //relinquishes most recent lock ... theLock.unlock(); //relinquishes the first lockUnless
theLock
was an NSRecursiveLock, a deadlock condition
would occur at the second lock
invocation in the example above.
An NSRecursiveLock keeps track of the recursion count: the number of
lock requests that the owning thread has made and not unlocked. This is also
the number of times unlock
must be invoked to return the lock. To access the
recursion count, use the recursionCount
method.
The NSLock, NSMultiReaderLock, and NSRecursiveLock classes all adopt the NSLocking protocol and offer various additional features and performance characteristics.
NSLock
,
NSMultiReaderLock
,
recursionCount()
Field Summary |
Fields inherited from interface com.webobjects.foundation.NSLocking |
OneCentury, OneDay, OneHour, OneMinute, OneSecond, OneWeek, OneYear |
Constructor Summary | |
NSRecursiveLock()
Creates an NSRecursiveLock. |
Method Summary | |
void |
lock()
Conformance to NSLocking. |
boolean |
lockBeforeDate(NSTimestamp timestamp)
Deprecated. Use tryLock(NSTimestamp) |
long |
recursionCount()
Gets this NSRecursiveLock object's recursion count. |
String |
toString()
|
boolean |
tryLock()
Attempts to acquire a lock. |
boolean |
tryLock(long msecs)
Attempts to acquire a lock for msecs milliseconds. |
boolean |
tryLock(NSTimestamp timestamp)
Attempts to acquire a lock until a specific time timestamp is reached. |
void |
unlock()
Attemps to relinquish a previously acquired lock by decreasing the recursion count by 1 . |
void |
unlock(long n)
Attemps to relinquish a previously acquired lock by decreasing the recursion count by n . |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public NSRecursiveLock()
Method Detail |
public void lock()
lock
in interface NSLocking
public boolean lockBeforeDate(NSTimestamp timestamp)
tryLock(NSTimestamp)
timestamp
- n/a
public long recursionCount()
0
if the current thread is not
the owner of this lockpublic String toString()
public boolean tryLock()
1
and returns.
If the current thread owns the lock, adds 1
to the recursion
count and returns.
true
if the lock was acquired; false
if another thread
owns the lockpublic boolean tryLock(long msecs)
msecs
milliseconds.
If the current thread owns the lock, adds 1
to the recursion count
and returns. Otherwise, the thread is blocked
until this NSRecursiveLock acquires the lock or msecs
milliseconds
have passed.
msecs
- time limit within which the lock has to be acquired
true
if the lock is acquired within this time limit;
false
if the time limit expires before a lock can be acquiredpublic boolean tryLock(NSTimestamp timestamp)
timestamp
is reached.
If the current thread owns the lock, adds 1
to the recursion
count and returns. Otherwise, the thread is
blocked until this NSRecursiveLock acquires the lock or timestamp
is reached.
timestamp
- input time within which lock must be acquired
true
if the lock is acquired before timestamp
is
reached; false
otherwisepublic void unlock()
1
. When the recursion count is 0
, this lock is
finally released so that other threads waiting can acquire a lock.
unlock
in interface NSLocking
IllegalStateException
- if the invoking thread is not the lock's owner
or if no lock has actually been acquiredlock()
public void unlock(long n)
n
. When the recursion count is 0
, this lock is
finally released so that other threads waiting can acquire a lock.
n
- amount to decrease the recursion count by
IllegalStateException
- if the invoking thread is not the lock's owner,
n
is a negative number or n
is greater than the number of time
lock()
was calledlock()
|
Last updated Thu Oct 21 15:04:16 PDT 2004. | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |