Rendezvous Synchronization
You can cause a runnable to interrupt itself by using the interrupt() method in the RWRunnableSelf class. This allows you to implement rendezvous or barrier synchronization. A rendezvous defines a meeting point between two threads. The thread that arrives at the meeting point first must wait for the other thread to arrive before continuing.
Example 9 shows how to use the interrupt() and requestInterrupt() calls to implement a simple rendezvous. The thread on one side of the rendezvous uses interrupt() to block itself while waiting for the other thread to arrive:
Example 9 – Implementing a simple rendezvous between threads
RWRunnableSelf self = rwRunnable();
while(/*condition*/) {
...
// Do something useful...
...
// Rendezvous with the other thread
// Wait here until the other thread releases the interrupt!
self.interrupt();
}
The thread on the other side of the rendezvous uses requestInterrupt() to block while waiting for the other thread to arrive:
// "other” points to the runnable containing the other thread
while(/*condition*/) {
...
// Do something useful...
...
// Rendezvous with the other thread
// Wait here until the other thread interrupts!
if (RW_THR_ACQUIRED == other.requestInterrupt())
// Must release twice because the thread was
// interrupted twice
{ other.releaseInterrupt();
other.releaseInterrupt(); }
}