cyclicbarrier.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 442 行 · 第 1/2 页
JAVA
442 行
*/
public CyclicBarrier(int parties, Runnable barrierAction) {
if (parties <= 0) throw new IllegalArgumentException();
this.parties = parties;
this.count = parties;
this.barrierCommand = barrierAction;
}
/**
* Creates a new <tt>CyclicBarrier</tt> that will trip when the
* given number of parties (threads) are waiting upon it, and
* does not perform a predefined action upon each barrier.
*
* @param parties the number of threads that must invoke {@link #await}
* before the barrier is tripped.
*
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1.
*/
public CyclicBarrier(int parties) {
this(parties, null);
}
/**
* Returns the number of parties required to trip this barrier.
* @return the number of parties required to trip this barrier.
**/
public int getParties() {
return parties;
}
/**
* Waits until all {@link #getParties parties} have invoked <tt>await</tt>
* on this barrier.
*
* <p>If the current thread is not the last to arrive then it is
* disabled for thread scheduling purposes and lies dormant until
* one of following things happens:
* <ul>
* <li>The last thread arrives; or
* <li>Some other thread {@link Thread#interrupt interrupts} the current
* thread; or
* <li>Some other thread {@link Thread#interrupt interrupts} one of the
* other waiting threads; or
* <li>Some other thread times out while waiting for barrier; or
* <li>Some other thread invokes {@link #reset} on this barrier.
* </ul>
* <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 waiting
* </ul>
* then {@link InterruptedException} is thrown and the current thread's
* interrupted status is cleared.
*
* <p>If the barrier is {@link #reset} while any thread is waiting, or if
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked,
* or while any thread is waiting,
* then {@link BrokenBarrierException} is thrown.
*
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting,
* then all other waiting threads will throw
* {@link BrokenBarrierException} and the barrier is placed in the broken
* state.
*
* <p>If the current thread is the last thread to arrive, and a
* non-null barrier action was supplied in the constructor, then the
* current thread runs the action before allowing the other threads to
* continue.
* If an exception occurs during the barrier action then that exception
* will be propagated in the current thread and the barrier is placed in
* the broken state.
*
* @return the arrival index of the current thread, where index
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and
* zero indicates the last to arrive.
*
* @throws InterruptedException if the current thread was interrupted
* while waiting
* @throws BrokenBarrierException if <em>another</em> thread was
* interrupted while the current thread was waiting, or the barrier was
* reset, or the barrier was broken when <tt>await</tt> was called,
* or the barrier action (if present) failed due an exception.
*/
public int await() throws InterruptedException, BrokenBarrierException {
try {
return dowait(false, 0L);
} catch (TimeoutException toe) {
throw new Error(toe); // cannot happen;
}
}
/**
* Waits until all {@link #getParties parties} have invoked <tt>await</tt>
* on this barrier.
*
* <p>If the current thread is not the last to arrive then it is
* disabled for thread scheduling purposes and lies dormant until
* one of the following things happens:
* <ul>
* <li>The last thread arrives; or
* <li>The specified timeout elapses; or
* <li>Some other thread {@link Thread#interrupt interrupts} the current
* thread; or
* <li>Some other thread {@link Thread#interrupt interrupts} one of the
* other waiting threads; or
* <li>Some other thread times out while waiting for barrier; or
* <li>Some other thread invokes {@link #reset} on this barrier.
* </ul>
* <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 waiting
* </ul>
* then {@link InterruptedException} is thrown and the current thread's
* interrupted status is cleared.
*
* <p>If the specified waiting time elapses then {@link TimeoutException}
* is thrown. If the time is less than or equal to zero, the
* method will not wait at all.
*
* <p>If the barrier is {@link #reset} while any thread is waiting, or if
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked,
* or while any thread is waiting,
* then {@link BrokenBarrierException} is thrown.
*
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting,
* then all other waiting threads will throw
* {@link BrokenBarrierException} and the barrier is placed in the broken
* state.
*
* <p>If the current thread is the last thread to arrive, and a
* non-null barrier action was supplied in the constructor, then the
* current thread runs the action before allowing the other threads to
* continue.
* If an exception occurs during the barrier action then that exception
* will be propagated in the current thread and the barrier is placed in
* the broken state.
*
* @param timeout the time to wait for the barrier
* @param unit the time unit of the timeout parameter
* @return the arrival index of the current thread, where index
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and
* zero indicates the last to arrive.
*
* @throws InterruptedException if the current thread was interrupted
* while waiting
* @throws TimeoutException if the specified timeout elapses.
* @throws BrokenBarrierException if <em>another</em> thread was
* interrupted while the current thread was waiting, or the barrier was
* reset, or the barrier was broken when <tt>await</tt> was called,
* or the barrier action (if present) failed due an exception.
*/
public int await(long timeout, TimeUnit unit)
throws InterruptedException,
BrokenBarrierException,
TimeoutException {
return dowait(true, unit.toNanos(timeout));
}
/**
* Queries if this barrier is in a broken state.
* @return <tt>true</tt> if one or more parties broke out of this
* barrier due to interruption or timeout since construction or
* the last reset, or a barrier action failed due to an exception;
* and <tt>false</tt> otherwise.
*/
public boolean isBroken() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return broken;
} finally {
lock.unlock();
}
}
/**
* Resets the barrier to its initial state. If any parties are
* currently waiting at the barrier, they will return with a
* {@link BrokenBarrierException}. Note that resets <em>after</em>
* a breakage has occurred for other reasons can be complicated to
* carry out; threads need to re-synchronize in some other way,
* and choose one to perform the reset. It may be preferable to
* instead create a new barrier for subsequent use.
*/
public void reset() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
/*
* Retract generation number enough to cover threads
* currently waiting on current and still resuming from
* previous generation, plus similarly accommodating spans
* after the reset.
*/
generation -= 4;
broken = false;
trip.signalAll();
} finally {
lock.unlock();
}
}
/**
* Returns the number of parties currently waiting at the barrier.
* This method is primarily useful for debugging and assertions.
*
* @return the number of parties currently blocked in {@link #await}
**/
public int getNumberWaiting() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return parties - count;
} finally {
lock.unlock();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?