java phaser: key methods - vanderbilt universityschmidt/cs891s/2020-pdfs/10.5.2-phaser-… · 4 key...
TRANSCRIPT
![Page 1: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/1.jpg)
Java Phaser:
Key Methods
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/2.jpg)
2
• Understand the structure & functionality of the Java Phaser barrier synchronizer
• Recognize the key methods in the Java Phaser
Learning Objectives in this Part of the Lesson
![Page 3: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/3.jpg)
3
Key Methods in Java Phaser
![Page 4: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/4.jpg)
4
Key Methods in Java Phaser• Phaser has a more complex API than
CountDownLatch or CyclicBarrier
• i.e., it has many methods that support a range of use cases
![Page 5: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/5.jpg)
5
Key Methods in Java Phaser• Phaser has a more complex API than
CountDownLatch or CyclicBarrier
• i.e., it has many methods that support a range of use cases
Fortunately, many of these methods are rarely used in practice
![Page 6: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/6.jpg)
6
• Constructor initializes the phase # to 0
Key Methods in Java Phaserpublic class Phaser {
...
public Phaser(int parties) {
...
}
public Phaser() { ... }
...
![Page 7: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/7.jpg)
7
• Constructor initializes the phase # to 0
• This constructor specifies the # of parties needed to advance to the next phase
Key Methods in Java Phaserpublic class Phaser {
...
public Phaser(int parties) {
...
}
public Phaser() { ... }
...
# of registered parties dictates when a phaser can advance to the next phase
![Page 8: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/8.jpg)
8
• Constructor initializes the phase # to 0
• This constructor specifies the # of parties needed to advance to the next phase
• This constructor is optional since parties can always register later
Key Methods in Java Phaserpublic class Phaser {
...
public Phaser(int parties) {
...
}
public Phaser() { ... }
...
With Java Phaser the # of parties need not match the # of threads
![Page 9: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/9.jpg)
9
• Constructor initializes the phase # to 0
• This constructor specifies the # of parties needed to advance to the next phase
• This constructor doesn’t specify any parties initially
Key Methods in Java Phaserpublic class Phaser {
...
public Phaser(int parties) {
...
}
public Phaser() { ... }
...
![Page 10: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/10.jpg)
10
• Constructor initializes the phase # to 0
• This constructor specifies the # of parties needed to advance to the next phase
• This constructor doesn’t specify any parties initially
• Any phaser created via this constructor therefore needs to register with it before using it
Key Methods in Java Phaserpublic class Phaser {
...
public Phaser(int parties) {
...
}
public Phaser() { ... }
...
![Page 11: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/11.jpg)
11
• Phaser’s key methods enable parties to register, synchronize, & terminate
Key Methods in Java Phaserpublic class Phaser {
...
public int register() { ... }
public int bulkRegister
(int parties) { ... }
public int
arriveAndAwaitAdvance()
{ ... }
public int ArriveAndDeregister()
{ ... }
protected boolean onAdvance
(int phase,
int registeredParties) {
return registeredParties == 0;
}
![Page 12: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/12.jpg)
12
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
Key Methods in Java Phaserpublic class Phaser {
...
public int register() { ... }
public int bulkRegister
(int parties) { ... }
# of registered parties dictates when a phaser can advance to the next phase
![Page 13: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/13.jpg)
13
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
Key Methods in Java Phaserpublic class Phaser {
...
public int arrive() { ... }
public int awaitAdvance
(int phase)
{ ... }
public int
arriveAndAwaitAdvance()
{ ... }
Having multiple methods provides flexibility wrt arrival & waiting to advance
![Page 14: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/14.jpg)
14
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrives at phaser, but does not block until other parties arrive
Key Methods in Java Phaserpublic class Phaser {
...
public int arrive() { ... }
![Page 15: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/15.jpg)
15
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrives at phaser, but does not block until other parties arrive
• Returns current phase # or a negative value if the phaser has already terminated
Key Methods in Java Phaserpublic class Phaser {
...
public int arrive() { ... }
This method is rarely used in practice
![Page 16: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/16.jpg)
16
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrives at phaser, but does not block until other parties arrive
• Blocks until the phase of this phaser advances from the given phase value
Key Methods in Java Phaserpublic class Phaser {
...
public int arrive() { ... }
public int awaitAdvance
(int phase)
{ ... }
![Page 17: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/17.jpg)
17
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrives at phaser, but does not block until other parties arrive
• Blocks until the phase of this phaser advances from the given phase value
• Returns immediately if current phase != given phase
Key Methods in Java Phaserpublic class Phaser {
...
public int arrive() { ... }
public int awaitAdvance
(int phase)
{ ... }
This method is rarely used in practice
![Page 18: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/18.jpg)
18
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrives at phaser, but does not block until other parties arrive
• Blocks until the phase of this phaser advances from the given phase value
• Arrives at phaser & blocks until other parties arrive
Key Methods in Java Phaserpublic class Phaser {
...
public int arrive() { ... }
public int awaitAdvance
(int phase)
{ ... }
public int
arriveAndAwaitAdvance()
{ ... }
Equivalent in effect to awaitAdvance(arrive())
![Page 19: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/19.jpg)
19
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrives at phaser, but does not block until other parties arrive
• Blocks until the phase of this phaser advances from the given phase value
• Arrives at phaser & blocks until other parties arrive
Key Methods in Java Phaserpublic class Phaser {
...
public int arrive() { ... }
public int awaitAdvance
(int phase)
{ ... }
public int
arriveAndAwaitAdvance()
{ ... }
This method is commonly used & is similar to await() on a Java CyclicBarrier
![Page 20: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/20.jpg)
20
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrive at the phaser & deregister without waiting for others to arrive
Key Methods in Java Phaserpublic class Phaser {
...
public int arriveAndDeregister()
{ ... }
![Page 21: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/21.jpg)
21
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrive at the phaser & deregister without waiting for others to arrive
• Reduces # of parties required to advance in future phases
Key Methods in Java Phaserpublic class Phaser {
...
public int arriveAndDeregister()
{ ... }
Often used by the party that controls the initialization of a phaser
![Page 22: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/22.jpg)
22
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrive at the phaser & deregister without waiting for others to arrive
• Hook method performs an action upon pending phase advance
Key Methods in Java Phaserpublic class Phaser {
...
protected boolean onAdvance
(int phase,
int registeredParties) {
return registeredParties == 0;
}
This method is invoked upon arrival of the party advancing the phaser
All other waiting parties are “dormant” when this hook method runs
![Page 23: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/23.jpg)
23
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrive at the phaser & deregister without waiting for others to arrive
• Hook method performs an action upon pending phase advance
Key Methods in Java Phaserpublic class Phaser {
...
protected boolean onAdvance
(int phase,
int registeredParties) {
return registeredParties == 0;
}
This hook method is similar to the barrier action on a Java CyclicBarrier
![Page 24: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/24.jpg)
24
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrive at the phaser & deregister without waiting for others to arrive
• Hook method performs an action upon pending phase advance
• Also initiates termination by returning a ‘true’ boolean value
Key Methods in Java Phaserpublic class Phaser {
...
protected boolean onAdvance
(int phase,
int registeredParties) {
return registeredParties == 0;
}
![Page 25: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/25.jpg)
25
• Phaser’s key methods enable parties to register, synchronize, & terminate
• Adds unarrived parties to phaser
• Arrive & await advance
• Arrive at the phaser & deregister without waiting for others to arrive
• Hook method performs an action upon pending phase advance
• Also initiates termination by returning a ‘true’ boolean value
Key Methods in Java Phaserpublic class Phaser {
...
protected boolean onAdvance
(int phase,
int registeredParties) {
return registeredParties == 0;
}
The default implementation terminates the phaser if there are no more registered parties
![Page 26: Java Phaser: Key Methods - Vanderbilt Universityschmidt/cs891s/2020-PDFs/10.5.2-Phaser-… · 4 Key Methods in Java Phaser •Phaser has a more complex API than CountDownLatch or](https://reader033.vdocuments.mx/reader033/viewer/2022060916/60a9babef99f93648e55be0d/html5/thumbnails/26.jpg)
26
End of Java Phaser:Key Methods