Java Reference
In-Depth Information
Typically, a party in a Phaser means a thread. However, a Phaser does not associate the registration of a party
with a specific thread. It simply maintains a count that is increased by one when a party is registered and decreased by
one when a party is deregistered.
The most important part of a Phaser is the way multiple parties synchronize on it. A typical way to synchronize
on a Phaser is to let the registered number of parties arrive and wait at the Phaser for other registered parties to arrive.
Once the last registered party arrives at the Phaser , all parties advance to the next phase of the Phaser .
The arriveAndAwaitAdvance() method of the Phaser class lets a party arrive at the Phaser and waits for other
parties to arrive before it can proceed.
The arriveAndDeregister() method of the Phaser class lets a party arrive at the Phaser and deregister without
waiting for other parties to arrive. Upon deregistration, the number of parties required to advance to the future phase
reduces by one. Typically, the arriveAndDeregister() method is used by a controller party whose job is to control
the advance of other parties without participating in the advance itself. Typically, the controller party registers itself
with the Phaser and waits for some conditions to occur; when the required condition occurs, it arrives and deregisters
itself from the Phaser so parties can synchronize on the Phaser and advance.
Let's walk through an example of using a Phaser to synchronize a group of tasks so they can all start at the same
time. An instance of the StartTogetherTask class, shown in Listing 6-36, represents a task in the example. This class
inherits from the Thread class. Its constructor accepts a task name and a Phaser instance. In its run() method, it prints
a message that it is initializing. It fakes its initialization by sleeping for a random period of 1 to 5 seconds. After that, it
prints a message that it is initialized. At this stage, it waits on a Phaser advance by calling the arriveAndAwaitAdvance()
method of the Phaser . This method will block until all registered parties arrive at the Phaser . When this method returns,
it prints a message that the task has started.
Listing 6-36. A StartTogetherTask Class to Represent Tasks That Start Together by Synchronizing on a Phaser
// StartTogetherTask.java
package com.jdojo.threads;
import java.util.Random;
import java.util.concurrent.Phaser;
public class StartTogetherTask extends Thread {
private Phaser phaser;
private String taskName;
private static Random rand = new Random();
public StartTogetherTask(String taskName, Phaser phaser) {
this.taskName = taskName;
this.phaser = phaser;
}
@Override
public void run() {
System.out.println(taskName + ":Initializing...");
// Sleep for some time between 1 and 5 seconds
int sleepTime = rand.nextInt(5) + 1;
try {
Thread.sleep(sleepTime * 1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
Search WWH ::




Custom Search