GeezerVison is showing docs.oracle.com — Phaser (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.util.concurrent
Class Phaser
-
-
Constructor Summary
Constructors
| Constructor |
Description |
Phaser() |
Creates a new phaser with no initially registered parties, no
parent, and initial phase number 0.
|
Phaser(int parties) |
Creates a new phaser with the given number of registered
unarrived parties, no parent, and initial phase number 0.
|
Phaser(Phaser parent) |
|
Phaser(Phaser parent,
int parties) |
Creates a new phaser with the given parent and number of
registered unarrived parties.
|
-
Method Summary
All Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
int |
arrive() |
Arrives at this phaser, without waiting for others to arrive.
|
int |
arriveAndAwaitAdvance() |
Arrives at this phaser and awaits others.
|
int |
arriveAndDeregister() |
Arrives at this phaser and deregisters from it without waiting
for others to arrive.
|
int |
awaitAdvance(int phase) |
Awaits the phase of this phaser to advance from the given phase
value, returning immediately if the current phase is not equal
to the given phase value or this phaser is terminated.
|
int |
awaitAdvanceInterruptibly(int phase) |
Awaits the phase of this phaser to advance from the given phase
value, throwing InterruptedException if interrupted
while waiting, or returning immediately if the current phase is
not equal to the given phase value or this phaser is
terminated.
|
int |
awaitAdvanceInterruptibly(int phase,
long timeout,
TimeUnit unit) |
Awaits the phase of this phaser to advance from the given phase
value or the given timeout to elapse, throwing InterruptedException if interrupted while waiting, or
returning immediately if the current phase is not equal to the
given phase value or this phaser is terminated.
|
int |
bulkRegister(int parties) |
Adds the given number of new unarrived parties to this phaser.
|
void |
forceTermination() |
Forces this phaser to enter termination state.
|
int |
getArrivedParties() |
Returns the number of registered parties that have arrived at
the current phase of this phaser.
|
Phaser |
getParent() |
Returns the parent of this phaser, or null if none.
|
int |
getPhase() |
Returns the current phase number.
|
int |
getRegisteredParties() |
Returns the number of parties registered at this phaser.
|
Phaser |
getRoot() |
Returns the root ancestor of this phaser, which is the same as
this phaser if it has no parent.
|
int |
getUnarrivedParties() |
Returns the number of registered parties that have not yet
arrived at the current phase of this phaser.
|
boolean |
isTerminated() |
Returns true if this phaser has been terminated.
|
protected boolean |
onAdvance(int phase,
int registeredParties) |
Overridable method to perform an action upon impending phase
advance, and to control termination.
|
int |
register() |
Adds a new unarrived party to this phaser.
|
String |
toString() |
Returns a string identifying this phaser, as well as its
state.
|
-
-
Constructor Detail
-
Phaser
public Phaser(int parties)
Creates a new phaser with the given number of registered
unarrived parties, no parent, and initial phase number 0.
- Parameters:
parties - the number of parties required to advance to the
next phase
- Throws:
IllegalArgumentException - if parties less than zero
or greater than the maximum number of parties supported
-
Phaser
public Phaser(Phaser parent,
int parties)
Creates a new phaser with the given parent and number of
registered unarrived parties. When the given parent is non-null
and the given number of parties is greater than zero, this
child phaser is registered with its parent.
- Parameters:
parent - the parent phaser
parties - the number of parties required to advance to the
next phase
- Throws:
IllegalArgumentException - if parties less than zero
or greater than the maximum number of parties supported
-
Method Detail
-
register
public int register()
Adds a new unarrived party to this phaser. If an ongoing
invocation of onAdvance(int, int) is in progress, this method
may await its completion before returning. If this phaser has
a parent, and this phaser previously had no registered parties,
this child phaser is also registered with its parent. If
this phaser is terminated, the attempt to register has
no effect, and a negative value is returned.
- Returns:
- the arrival phase number to which this registration
applied. If this value is negative, then this phaser has
terminated, in which case registration has no effect.
- Throws:
IllegalStateException - if attempting to register more
than the maximum supported number of parties
-
arrive
public int arrive()
Arrives at this phaser, without waiting for others to arrive.
It is a usage error for an unregistered party to invoke this
method. However, this error may result in an IllegalStateException only upon some subsequent operation on
this phaser, if ever.
- Returns:
- the arrival phase number, or a negative value if terminated
- Throws:
IllegalStateException - if not terminated and the number
of unarrived parties would become negative
-
awaitAdvance
public int awaitAdvance(int phase)
Awaits the phase of this phaser to advance from the given phase
value, returning immediately if the current phase is not equal
to the given phase value or this phaser is terminated.
- Parameters:
phase - an arrival phase number, or negative value if
terminated; this argument is normally the value returned by a
previous call to arrive or arriveAndDeregister.
- Returns:
- the next arrival phase number, or the argument if it is
negative, or the (negative) current phase
if terminated
-
awaitAdvanceInterruptibly
public int awaitAdvanceInterruptibly(int phase)
throws InterruptedException
Awaits the phase of this phaser to advance from the given phase
value, throwing InterruptedException if interrupted
while waiting, or returning immediately if the current phase is
not equal to the given phase value or this phaser is
terminated.
- Parameters:
phase - an arrival phase number, or negative value if
terminated; this argument is normally the value returned by a
previous call to arrive or arriveAndDeregister.
- Returns:
- the next arrival phase number, or the argument if it is
negative, or the (negative) current phase
if terminated
- Throws:
InterruptedException - if thread interrupted while waiting
-
awaitAdvanceInterruptibly
public int awaitAdvanceInterruptibly(int phase,
long timeout,
TimeUnit unit)
throws InterruptedException,
TimeoutException
Awaits the phase of this phaser to advance from the given phase
value or the given timeout to elapse, throwing InterruptedException if interrupted while waiting, or
returning immediately if the current phase is not equal to the
given phase value or this phaser is terminated.
- Parameters:
phase - an arrival phase number, or negative value if
terminated; this argument is normally the value returned by a
previous call to arrive or arriveAndDeregister.
timeout - how long to wait before giving up, in units of
unit
unit - a TimeUnit determining how to interpret the
timeout parameter
- Returns:
- the next arrival phase number, or the argument if it is
negative, or the (negative) current phase
if terminated
- Throws:
InterruptedException - if thread interrupted while waiting
TimeoutException - if timed out while waiting
-
forceTermination
public void forceTermination()
Forces this phaser to enter termination state. Counts of
registered parties are unaffected. If this phaser is a member
of a tiered set of phasers, then all of the phasers in the set
are terminated. If this phaser is already terminated, this
method has no effect. This method may be useful for
coordinating recovery after one or more tasks encounter
unexpected exceptions.
-
getPhase
public final int getPhase()
Returns the current phase number. The maximum phase number is
Integer.MAX_VALUE, after which it restarts at
zero. Upon termination, the phase number is negative,
in which case the prevailing phase prior to termination
may be obtained via getPhase() + Integer.MIN_VALUE.
- Returns:
- the phase number, or a negative value if terminated
-
getArrivedParties
public int getArrivedParties()
Returns the number of registered parties that have arrived at
the current phase of this phaser. If this phaser has terminated,
the returned value is meaningless and arbitrary.
- Returns:
- the number of arrived parties
-
getUnarrivedParties
public int getUnarrivedParties()
Returns the number of registered parties that have not yet
arrived at the current phase of this phaser. If this phaser has
terminated, the returned value is meaningless and arbitrary.
- Returns:
- the number of unarrived parties
-
toString
public String toString()
Returns a string identifying this phaser, as well as its
state. The state, in brackets, includes the String "phase = " followed by the phase number, "parties = "
followed by the number of registered parties, and "arrived = " followed by the number of arrived parties.
- Overrides:
toString in class Object
- Returns:
- a string identifying this phaser, as well as its state
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|