Java Reference
In-Depth Information
Phaser has three types of parties count: a registered parties count, an arrived parties count,
and an unarrived parties count. The registered parties count is the number of parties that are
registered for synchronization. The arrived parties count is the number of parties that have
arrived at the current phase of the phaser. The unarrived parties count is the number of parties
that have not yet arrived at the current phase of the phaser. When the last party arrives, the
phaser advances to the next phase. Note that all three types of party counts are dynamic.
A
Optionally, a
Phaser lets you execute a phaser action when all registered parties arrive at the
phaser. Recall that a CyclicBarrier lets you execute a barrier action, which is a Runnable
task. Unlike a CyclicBarrier , you specify a phaser action by writing code in the onAdvance()
method of your Phaser class. It means you need to use your own Phaser class by inheriting it
from the Phaser class and override the onAdvance() method to provide a Phaser action. I will
discuss an example of this kind shortly.
Figure 6-12 shows a phaser with three phases. It synchronizes on different number of parties in each phase.
An arrow in the figure represents a party.
Phase-0
Phase-1
Phase-2
P
H
A
S
E
R
P
H
A
S
E
R
P
H
A
S
E
R
Figure 6-12. A Phaser with three phases with a different number of parties in each phase
There are several steps to work with a Phaser . You can create a Phaser with no initially registered party using its
default constructor.
Phaser phaser = new Phaser(); // A phaser with no registered parties
Another constructor lets you register parties when the Phaser is created.
Phaser phaser = new Phaser(5); // A phaser with 5 registered parties
A Phaser may be arranged in a tree-like structure. Other constructors let you create a Phaser by specifying the
parent of the newly created Phaser .
Once you have created a Phaser , the next step is to register parties that are interested in synchronizing on the
phaser. You can register a party with a Phaser in the following ways:
Phaser class when you
By specifying the number of parties to register in the constructor of the
create a Phaser object
register() method of the Phaser class to register one party at a time
By using the
bulkRegister(int parties) method of the Phaser class to register the specified
number of parties in bulk
The registered parties of a Phaser may change at any time by registering new parties or deregistering the already
registered parties. You can deregister a registered party using the arriveAndDeregister() method of the Phaser class.
This method lets a party arrive at the Phaser and deregister without waiting for other parties to arrive. If a party is
deregistered, the number of parties is reduced by one in the next phase of the Phaser .
By using the
 
Search WWH ::




Custom Search