reentrantlock.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 874 行 · 第 1/3 页
JAVA
874 行
holds_ = 1;
return true;
}
else if (caller == owner_) {
++holds_;
return true;
}
}
WaitQueue.WaitNode n = new WaitQueue.WaitNode();
return n.doTimedWait(this, nanos);
}
protected synchronized WaitQueue.WaitNode getSignallee(Thread caller) {
if (caller != owner_)
throw new IllegalMonitorStateException("Not owner");
// assert (holds_ > 0)
if (holds_ >= 2) { // current thread will keep the lock
--holds_;
return null;
}
// assert (holds_ == 1)
WaitQueue.WaitNode w = wq_.extract();
if (w == null) { // if none, clear for new arrivals
owner_ = null;
holds_ = 0;
}
return w;
}
public void unlock() {
Thread caller = Thread.currentThread();
for (;;) {
WaitQueue.WaitNode w = getSignallee(caller);
if (w == null) return; // no one to signal
if (w.signal(this)) return; // notify if still waiting, else skip
}
}
public final boolean isFair() {
return true;
}
public synchronized boolean hasQueuedThreads() {
return wq_.hasNodes();
}
public synchronized int getQueueLength() {
return wq_.getLength();
}
public synchronized Collection getQueuedThreads() {
return wq_.getWaitingThreads();
}
public synchronized boolean isQueued(Thread thread) {
return wq_.isWaiting(thread);
}
}
/**
* Creates an instance of <tt>ReentrantLock</tt>.
* This is equivalent to using <tt>ReentrantLock(false)</tt>.
*/
public ReentrantLock() {
impl = new NonfairImpl();
}
/**
* Creates an instance of <tt>ReentrantLock</tt> with the
* given fairness policy.
* @param fair true if this lock will be fair; else false
*/
public ReentrantLock(boolean fair) {
impl = (fair)? (Impl)new FairImpl() : new NonfairImpl();
}
/**
* Acquires the lock.
*
* <p>Acquires the lock if it is not held by another thread and returns
* immediately, setting the lock hold count to one.
*
* <p>If the current thread
* already holds the lock then the hold count is incremented by one and
* the method returns immediately.
*
* <p>If the lock is held by another thread then the
* current thread becomes disabled for thread scheduling
* purposes and lies dormant until the lock has been acquired,
* at which time the lock hold count is set to one.
*/
public void lock() {
boolean wasInterrupted = false;
while (true) {
try {
impl.lockInterruptibly();
if (wasInterrupted) {
Thread.currentThread().interrupt();
}
return;
}
catch (InterruptedException e) {
wasInterrupted = true;
}
}
}
/**
* Acquires the lock unless the current thread is
* {@link Thread#interrupt interrupted}.
*
* <p>Acquires the lock if it is not held by another thread and returns
* immediately, setting the lock hold count to one.
*
* <p>If the current thread already holds this lock then the hold count
* is incremented by one and the method returns immediately.
*
* <p>If the lock is held by another thread then the
* current thread becomes disabled for thread scheduling
* purposes and lies dormant until one of two things happens:
*
* <ul>
*
* <li>The lock is acquired by the current thread; or
*
* <li>Some other thread {@link Thread#interrupt interrupts} the current
* thread.
*
* </ul>
*
* <p>If the lock is acquired by the current thread then the lock hold
* count is set to one.
*
* <p>If the current thread:
*
* <ul>
*
* <li>has its interrupted status set on entry to this method; or
*
* <li>is {@link Thread#interrupt interrupted} while acquiring
* the lock,
*
* </ul>
*
* then {@link InterruptedException} is thrown and the current thread's
* interrupted status is cleared.
*
* <p>In this implementation, as this method is an explicit interruption
* point, preference is
* given to responding to the interrupt over normal or reentrant
* acquisition of the lock.
*
* @throws InterruptedException if the current thread is interrupted
*/
public void lockInterruptibly() throws InterruptedException {
impl.lockInterruptibly();
}
/**
* Acquires the lock only if it is not held by another thread at the time
* of invocation.
*
* <p>Acquires the lock if it is not held by another thread and
* returns immediately with the value <tt>true</tt>, setting the
* lock hold count to one. Even when this lock has been set to use a
* fair ordering policy, a call to <tt>tryLock()</tt> <em>will</em>
* immediately acquire the lock if it is available, whether or not
* other threads are currently waiting for the lock.
* This "barging" behavior can be useful in certain
* circumstances, even though it breaks fairness. If you want to honor
* the fairness setting for this lock, then use
* {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }
* which is almost equivalent (it also detects interruption).
*
* <p> If the current thread
* already holds this lock then the hold count is incremented by one and
* the method returns <tt>true</tt>.
*
* <p>If the lock is held by another thread then this method will return
* immediately with the value <tt>false</tt>.
*
* @return <tt>true</tt> if the lock was free and was acquired by the
* current thread, or the lock was already held by the current thread; and
* <tt>false</tt> otherwise.
*/
public boolean tryLock() {
return impl.tryLock();
}
/**
* Acquires the lock if it is not held by another thread within the given
* waiting time and the current thread has not been
* {@link Thread#interrupt interrupted}.
*
* <p>Acquires the lock if it is not held by another thread and returns
* immediately with the value <tt>true</tt>, setting the lock hold count
* to one. If this lock has been set to use a fair ordering policy then
* an available lock <em>will not</em> be acquired if any other threads
* are waiting for the lock. This is in contrast to the {@link #tryLock()}
* method. If you want a timed <tt>tryLock</tt> that does permit barging on
* a fair lock then combine the timed and un-timed forms together:
*
* <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
* </pre>
*
* <p>If the current thread
* already holds this lock then the hold count is incremented by one and
* the method returns <tt>true</tt>.
*
* <p>If the lock is held by another thread then the
* current thread becomes disabled for thread scheduling
* purposes and lies dormant until one of three things happens:
*
* <ul>
*
* <li>The lock is acquired by the current thread; or
*
* <li>Some other thread {@link Thread#interrupt interrupts} the current
* thread; or
*
* <li>The specified waiting time elapses
*
* </ul>
*
* <p>If the lock is acquired then the value <tt>true</tt> is returned and
* the lock hold count is set to one.
*
* <p>If the current thread:
*
* <ul>
*
* <li>has its interrupted status set on entry to this method; or
*
* <li>is {@link Thread#interrupt interrupted} while acquiring
* the lock,
*
* </ul>
* then {@link InterruptedException} is thrown and the current thread's
* interrupted status is cleared.
*
* <p>If the specified waiting time elapses then the value <tt>false</tt>
* is returned.
* If the time is
* less than or equal to zero, the method will not wait at all.
*
* <p>In this implementation, as this method is an explicit interruption
* point, preference is
* given to responding to the interrupt over normal or reentrant
* acquisition of the lock, and over reporting the elapse of the waiting
* time.
*
* @param timeout the time to wait for the lock
* @param unit the time unit of the timeout argument
*
* @return <tt>true</tt> if the lock was free and was acquired by the
* current thread, or the lock was already held by the current thread; and
* <tt>false</tt> if the waiting time elapsed before the lock could be
* acquired.
*
* @throws InterruptedException if the current thread is interrupted
* @throws NullPointerException if unit is null
*
*/
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
return impl.tryLock(unit.toNanos(timeout));
}
/**
* Attempts to release this lock.
*
* <p>If the current thread is the
* holder of this lock then the hold count is decremented. If the
* hold count is now zero then the lock is released. If the
* current thread is not the holder of this lock then {@link
* IllegalMonitorStateException} is thrown.
* @throws IllegalMonitorStateException if the current thread does not
* hold this lock.
*/
public void unlock() {
impl.unlock();
}
/**
* Returns a {@link Condition} instance for use with this
* {@link Lock} instance.
*
* <p>The returned {@link Condition} instance supports the same
* usages as do the {@link Object} monitor methods ({@link
* Object#wait() wait}, {@link Object#notify notify}, and {@link
* Object#notifyAll notifyAll}) when used with the built-in
* monitor lock.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?