| java.lang.Object EDU.oswego.cs.dl.util.concurrent.Rendezvous
Rendezvous | public class Rendezvous implements Barrier(Code) | | A rendezvous is a barrier that:
- Unlike a CyclicBarrier, is not restricted to use
with fixed-sized groups of threads.
Any number of threads can attempt to enter a rendezvous,
but only the predetermined number of parties enter
and later become released from the rendezvous at any give time.
- Enables each participating thread to exchange information
with others at the rendezvous point. Each entering thread
presents some object on entry to the rendezvous, and
returns some object on release. The object returned is
the result of a RendezvousFunction that is run once per
rendezvous, (it is run by the last-entering thread). By
default, the function applied is a rotation, so each
thread returns the object given by the next (modulo parties)
entering thread. This default function faciliates simple
application of a common use of rendezvous, as exchangers.
Rendezvous use an all-or-none breakage model
for failed synchronization attempts: If threads
leave a rendezvous point prematurely because of timeout
or interruption, others will also leave abnormally
(via BrokenBarrierException), until
the rendezvous is restart ed. This is usually
the simplest and best strategy for sharing knowledge
about failures among cooperating threads in the most
common usages contexts of Rendezvous.
While any positive number (including 1) of parties can
be handled, the most common case is to have two parties.
Sample Usage
Here are the highlights of a class that uses a Rendezvous to
swap buffers between threads so that the thread filling the
buffer gets a freshly
emptied one when it needs it, handing off the filled one to
the thread emptying the buffer.
class FillAndEmpty {
Rendezvous exchanger = new Rendezvous(2);
Buffer initialEmptyBuffer = ... a made-up type
Buffer initialFullBuffer = ...
class FillingLoop implements Runnable {
public void run() {
Buffer currentBuffer = initialEmptyBuffer;
try {
while (currentBuffer != null) {
addToBuffer(currentBuffer);
if (currentBuffer.full())
currentBuffer = (Buffer)(exchanger.rendezvous(currentBuffer));
}
}
catch (BrokenBarrierException ex) {
return;
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
class EmptyingLoop implements Runnable {
public void run() {
Buffer currentBuffer = initialFullBuffer;
try {
while (currentBuffer != null) {
takeFromBuffer(currentBuffer);
if (currentBuffer.empty())
currentBuffer = (Buffer)(exchanger.rendezvous(currentBuffer));
}
}
catch (BrokenBarrierException ex) {
return;
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
void start() {
new Thread(new FillingLoop()).start();
new Thread(new EmptyingLoop()).start();
}
}
[ Introduction to this package. ]
|
Inner Class :public interface RendezvousFunction | |
Inner Class :public static class Rotator implements RendezvousFunction | |
Constructor Summary | |
public | Rendezvous(int parties) Create a Barrier for the indicated number of parties,
and the default Rotator function to run at each barrier point. | public | Rendezvous(int parties, RendezvousFunction function) Create a Barrier for the indicated number of parties. |
Method Summary | |
public Object | attemptRendezvous(Object x, long msecs) Wait msecs to complete a rendezvous.
Parameters: x - the item to present at rendezvous point. | public synchronized boolean | broken() | protected Object | doRendezvous(Object x, boolean timed, long msecs) | public int | parties() | public Object | rendezvous(Object x) Enter a rendezvous; returning after all other parties arrive.
Parameters: x - the item to present at rendezvous point. | public void | restart() Reset to initial state. | public synchronized RendezvousFunction | setRendezvousFunction(RendezvousFunction function) Set the function to call at the point at which all threads reach the
rendezvous. |
broken_ | protected boolean broken_(Code) | | |
departures_ | protected long departures_(Code) | | Number of threads that are permitted to depart rendezvous
|
entries_ | protected int entries_(Code) | | Number of threads that have entered rendezvous
|
entryGate_ | final protected Semaphore entryGate_(Code) | | Incoming threads pile up on entry until last set done.
|
parties_ | final protected int parties_(Code) | | |
rendezvousFunction_ | protected RendezvousFunction rendezvousFunction_(Code) | | The function to run at rendezvous point
|
slots_ | final protected Object[] slots_(Code) | | Temporary holder for items in exchange
|
Rendezvous | public Rendezvous(int parties)(Code) | | Create a Barrier for the indicated number of parties,
and the default Rotator function to run at each barrier point.
exception: IllegalArgumentException - if parties less than or equal to zero. |
Rendezvous | public Rendezvous(int parties, RendezvousFunction function)(Code) | | Create a Barrier for the indicated number of parties.
and the given function to run at each barrier point.
exception: IllegalArgumentException - if parties less than or equal to zero. |
attemptRendezvous | public Object attemptRendezvous(Object x, long msecs) throws InterruptedException, TimeoutException, BrokenBarrierException(Code) | | Wait msecs to complete a rendezvous.
Parameters: x - the item to present at rendezvous point. By default, this item is exchanged with another. Parameters: msecs - The maximum time to wait. an item x given by some thread, and/or processedby the rendezvousFunction. exception: BrokenBarrierException - if any other threadin any previous or current barrier since either creation or the last restart operation left the barrierprematurely due to interruption or time-out. (If so,the broken status is also set.) Also returns asbroken if the RendezvousFunction encountered a run-time exception.Threads that are noticed to have beeninterrupted after being released are not consideredto have broken the barrier.In all cases, the interruptionstatus of the current thread is preserved, so can be testedby checking Thread.interrupted . exception: InterruptedException - if this thread was interruptedduring the exchange. If so, broken status is also set. exception: TimeoutException - if this thread timed out waiting forthe exchange. If the timeout occured while already in theexchange, broken status is also set. |
broken | public synchronized boolean broken()(Code) | | |
parties | public int parties()(Code) | | |
rendezvous | public Object rendezvous(Object x) throws InterruptedException, BrokenBarrierException(Code) | | Enter a rendezvous; returning after all other parties arrive.
Parameters: x - the item to present at rendezvous point. By default, this item is exchanged with another. an item x given by some thread, and/or processedby the rendezvousFunction. exception: BrokenBarrierException - if any other threadin any previous or current barrier since either creation or the last restart operation left the barrierprematurely due to interruption or time-out. (If so,the broken status is also set.) Also returns asbroken if the RendezvousFunction encountered a run-time exception.Threads that are noticed to have beeninterrupted after being released are not consideredto have broken the barrier.In all cases, the interruptionstatus of the current thread is preserved, so can be testedby checking Thread.interrupted . exception: InterruptedException - if this thread was interruptedduring the exchange. If so, broken status is also set. |
restart | public void restart()(Code) | | Reset to initial state. Clears both the broken status
and any record of waiting threads, and releases all
currently waiting threads with indeterminate return status.
This method is intended only for use in recovery actions
in which it is somehow known
that no thread could possibly be relying on the
the synchronization properties of this barrier.
|
setRendezvousFunction | public synchronized RendezvousFunction setRendezvousFunction(RendezvousFunction function)(Code) | | Set the function to call at the point at which all threads reach the
rendezvous. This function is run exactly once, by the thread
that trips the barrier. The function is not run if the barrier is
broken.
Parameters: function - the function to run. If null, no function is run. the previous function |
|
|