PATH |
NSRecursiveLock
- Inherits from:
- Object
- Implements:
- NSLocking
- Package:
- com.webobjects.foundation
Class Description
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 lock */
Unless theLock was an NSRecursiveLock, a deadlock condition would occur at the second lock message in the example above.
The NSRecursiveLock object 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. See the NSLock and NSMultiReaderLock class descriptions for more information.
Method Types
- Constructors
- NSRecursiveLock
- Instance methods
- lock
- tryLock
- lockBeforeDate
- recursionCount
- toString
- unlock
Constructors
NSRecursiveLock
public NSRecursiveLock()
Instance Methods
lock
public void lock()
lockBeforeDate
public boolean lockBeforeDate(NSTimestamp timestamp)
tryLock(NSTimestamp timestamp)
instead.
recursionCount
public synchronized long recursionCount()
toString
public String toString()
tryLock
public boolean tryLock()
true
. If the current thread owns the lock, increments the recursion count and returns with a value of true
. If the another thread owns the lock, returns false
immediately.
public boolean tryLock(long msec)
true
. Otherwise, the thread is blocked until the receiver acquires the lock or msec milliseconds have passed. Returns true
if the lock is acquired within this time limit. Returns false
if the time limit expires before a lock can be acquired.
public boolean tryLock(NSTimestamp timestamp)
true
. Otherwise, the thread is blocked until the receiver acquires the lock or timestamp is reached. Returns true
if the lock is acquired within this time limit. Returns false
if the time limit expires before a lock can be acquired.
unlock
public synchronized void unlock()
public synchronized void unlock(long levels)
© 2001 Apple Computer, Inc. (Last Published April 17, 2001)