![Page 1: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/1.jpg)
Java CyclicBarrier:
Example Application
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/2.jpg)
2
• Understand the structure & functionality of Java CyclicBarrier
• Recognize the key methods in the Java CyclicBarrier
• Know how to program with Java CyclicBarrier in practice
Learning Objectives in this Part of the Lesson
class GCDCyclicBarrierWorker implements Runnable {
private final CyclicBarrier mEntryBarrier;
private final CyclicBarrier mExitBarrier; ...
GCDCyclicBarrierWorker(CyclicBarrier entryBarrier,
CyclicBarrier exitBarrier, ...) {
mEntryBarrier = entryBarrier; mExitBarrier = exitBarrier;
...
}
public void run() {
...
mEntryBarrier.await();
runTest();
mExitBarrier.await();
...
![Page 3: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/3.jpg)
3
Overview of the GCD App
![Page 4: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/4.jpg)
4
Overview of the GCD App• This Android app uses CyclicBarrier objects to
coordinate the concurrent benchmarking of fourGreatest Common Divisor (GCD) algorithms
See github.com/douglascraigschmidt/POSA/tree/master/ex/M3/GCD/CyclicBarrier
![Page 5: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/5.jpg)
5
• This Android app uses CyclicBarrier objects to coordinate the concurrent benchmarking of fourGreatest Common Divisor (GCD) algorithms
• GCD computes the largest positive integer that is a divisor of two numbers
• e.g., the GCD of 80 & 120 = 40
See en.wikipedia.org/wiki/Greatest_common_divisor
Overview of the GCD App
![Page 6: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/6.jpg)
6
• This Android app uses CyclicBarrier objects to coordinate the concurrent benchmarking of fourGreatest Common Divisor (GCD) algorithms
• GCD computes the largest positive integer that is a divisor of two numbers
• Four GCD algorithms are tested
Overview of the GCD App
![Page 7: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/7.jpg)
7
• This Android app uses CyclicBarrier objects to coordinate the concurrent benchmarking of fourGreatest Common Divisor (GCD) algorithms
• GCD computes the largest positive integer that is a divisor of two numbers
• Four GCD algorithms are tested
• The gcd() method defined by BigInteger
See docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#gcd
Overview of the GCD App
![Page 8: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/8.jpg)
8
• This Android app uses CyclicBarrier objects to coordinate the concurrent benchmarking of fourGreatest Common Divisor (GCD) algorithms
• GCD computes the largest positive integer that is a divisor of two numbers
• Four GCD algorithms are tested
• The gcd() method defined by BigInteger
• An iterative Euclid algorithm
See en.wikipedia.org/wiki/Euclidean_algorithm
Overview of the GCD App
![Page 9: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/9.jpg)
9
• This Android app uses CyclicBarrier objects to coordinate the concurrent benchmarking of fourGreatest Common Divisor (GCD) algorithms
• GCD computes the largest positive integer that is a divisor of two numbers
• Four GCD algorithms are tested
• The gcd() method defined by BigInteger
• An iterative Euclid algorithm
• A recursive Euclid algorithm
See codedost.com/java/methods-and-recursion-in-java/java-program-to-find-gcd-hcf-using-euclidean-algorithm-using-recursion
Overview of the GCD App
![Page 10: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/10.jpg)
10
• This Android app uses CyclicBarrier objects to coordinate the concurrent benchmarking of fourGreatest Common Divisor (GCD) algorithms
• GCD computes the largest positive integer that is a divisor of two numbers
• Four GCD algorithms are tested
• The gcd() method defined by BigInteger
• An iterative Euclid algorithm
• A recursive Euclid algorithm
• A complex GCD algorithm that uses binary arithmetic
See en.wikipedia.org/wiki/Binary_GCD_algorithm
Overview of the GCD App
![Page 11: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/11.jpg)
11
• This Android app uses CyclicBarrier objects to coordinate the concurrent benchmarking of fourGreatest Common Divisor (GCD) algorithms
• GCD computes the largest positive integer that is a divisor of two numbers
• Four GCD algorithms are tested
• The gcd() method defined by BigInteger
• An iterative Euclid algorithm
• A recursive Euclid algorithm
• A complex GCD algorithm that uses binary arithmetic
However, the details of these algorithms are not important for our discussion
Overview of the GCD App
![Page 12: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/12.jpg)
12
GCDCyclicBarrierTestClass Walkthrough
![Page 13: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/13.jpg)
13
GCDCyclicBarrierTest Class Walkthrough
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
• Create worker threads that use exit & entry barrier CyclicBarrier objects
See GCD/CyclicBarrier/app/src/test/java/edu/vandy/gcdtesttask/GCDCyclicBarrierTest.java
![Page 14: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/14.jpg)
14
GCDCyclicBarrierTest Class Walkthrough
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
• Create worker threads that use exit & entry barrier CyclicBarrier objects
Entry point into the unit test
![Page 15: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/15.jpg)
15
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Initialize all the GCD algorithms
![Page 16: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/16.jpg)
16
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Create entry barrier
We add a "+ 1" for the thread that initializes the tests
![Page 17: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/17.jpg)
17
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Barrier action allocates each cycle’s input
![Page 18: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/18.jpg)
18
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Create exit barrier
We add a "+ 1" for the thread that initializes the tests
![Page 19: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/19.jpg)
19
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Iterate through each cycle
![Page 20: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/20.jpg)
20
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Create & start threads w/barriers
![Page 21: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/21.jpg)
21
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Don't start just yet
![Page 22: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/22.jpg)
22
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Let all worker threads proceed at the same time, fixing
limitation with CountDownLatch
See previous lesson on “Java CountDownLatch”
![Page 23: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/23.jpg)
23
GCDCyclicBarrierTest Class Walkthrough• Create worker threads that use exit & entry barrier CyclicBarrier objects
Exit barrier waits for all threads to finish this cycle
class GCDCyclicBarrierTest {
@Test public void testGCDCyclicBarrierTester() {
List<GCDTuple> gcdTests = makeGCDTuples();
CyclicBarrier entryBarrier =
new CyclicBarrier(gcdTests.size() + 1, () ->
GCDCyclicBarrierWorker.initializeInput(sITERATIONS));
CyclicBarrier exitBarrier =
new CyclicBarrier(gcdTests.size() + 1);
for (int cycle = 1; cycle <= sCYCLES; cycle++) {
gcdTests.forEach(gcdTuple -> new Thread(new
GCDCyclicBarrierWorker(entryBarrier, exitBarrier,
gcdTuple, this)).start());
System.out.println("Starting tests");
entryBarrier.await();
System.out.println("Waiting for results");
exitBarrier.await();
System.out.println("All tests done"); ...
After await() returns for a CyclicBarrier it will be reset (& is thus reusable) without needing to create a new CyclicBarrier instance
![Page 24: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/24.jpg)
24
GCDCyclicBarrierWorkerClass Walkthrough
![Page 25: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/25.jpg)
25
GCDCyclicBarrierWorker Class Walkthrough
class GCDCyclicBarrierWorker implements Runnable {
private final CyclicBarrier mEntryBarrier;
private final CyclicBarrier mExitBarrier;
...
GCDCyclicBarrierWorker(CyclicBarrier entryBarrier,
CyclicBarrier exitBarrier, ...) {
mEntryBarrier = entryBarrier; mExitBarrier = exitBarrier;
...
}
public void run() {
...
mEntryBarrier.await();
runTest();
mExitBarrier.await();
...
• This class applies two entry & exit barrier CyclicBarrier objects to coordinate the benchmarking of a given GCD algorithm implementation
Define a worker that runs in a thread
See GCD/CyclicBarrier/app/src/main/java/edu/vandy/gcdtesttask/presenter/GCDCyclicBarrierWorker.java
![Page 26: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/26.jpg)
26
class GCDCyclicBarrierWorker implements Runnable {
private final CyclicBarrier mEntryBarrier;
private final CyclicBarrier mExitBarrier;
...
GCDCyclicBarrierWorker(CyclicBarrier entryBarrier,
CyclicBarrier exitBarrier, ...) {
mEntryBarrier = entryBarrier; mExitBarrier = exitBarrier;
...
}
public void run() {
...
mEntryBarrier.await();
runTest();
mExitBarrier.await();
...
GCDCyclicBarrierWorker Class Walkthrough• This class applies two entry & exit barrier CyclicBarrier objects to coordinate
the benchmarking of a given GCD algorithm implementation
Initialize barrier fields
![Page 27: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/27.jpg)
27
class GCDCyclicBarrierWorker implements Runnable {
private final CyclicBarrier mEntryBarrier;
private final CyclicBarrier mExitBarrier;
...
GCDCyclicBarrierWorker(CyclicBarrier entryBarrier,
CyclicBarrier exitBarrier, ...) {
mEntryBarrier = entryBarrier; mExitBarrier = exitBarrier;
...
}
public void run() {
...
mEntryBarrier.await();
runTest();
mExitBarrier.await();
...
GCDCyclicBarrierWorker Class Walkthrough• This class applies two entry & exit barrier CyclicBarrier objects to coordinate
the benchmarking of a given GCD algorithm implementation
This hook method executes after the thread is started
![Page 28: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/28.jpg)
28
GCDCyclicBarrierWorker Class Walkthrough• This class applies two entry & exit barrier CyclicBarrier objects to coordinate
the benchmarking of a given GCD algorithm implementation
class GCDCyclicBarrierWorker implements Runnable {
private final CyclicBarrier mEntryBarrier;
private final CyclicBarrier mExitBarrier;
...
GCDCyclicBarrierWorker(CyclicBarrier entryBarrier,
CyclicBarrier exitBarrier, ...) {
mEntryBarrier = entryBarrier; mExitBarrier = exitBarrier;
...
}
public void run() {
...
mEntryBarrier.await();
runTest();
mExitBarrier.await();
...
This entry barrier causes all worker threads to wait until they are all ready, thus fixing the earlier limitation with CountDownLatch
See previous lesson on “Java CountDownLatch”
![Page 29: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/29.jpg)
29
class GCDCyclicBarrierWorker implements Runnable {
private final CyclicBarrier mEntryBarrier;
private final CyclicBarrier mExitBarrier;
...
GCDCyclicBarrierWorker(CyclicBarrier entryBarrier,
CyclicBarrier exitBarrier, ...) {
mEntryBarrier = entryBarrier; mExitBarrier = exitBarrier;
...
}
public void run() {
...
mEntryBarrier.await();
runTest();
mExitBarrier.await();
...
GCDCyclicBarrierWorker Class Walkthrough• This class applies two entry & exit barrier CyclicBarrier objects to coordinate
the benchmarking of a given GCD algorithm implementation
Run the GCD algorithm associated with this object
![Page 30: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/30.jpg)
30
GCDCyclicBarrierWorker Class Walkthrough• This class applies two entry & exit barrier CyclicBarrier objects to coordinate
the benchmarking of a given GCD algorithm implementation
class GCDCyclicBarrierWorker implements Runnable {
private final CyclicBarrier mEntryBarrier;
private final CyclicBarrier mExitBarrier;
...
GCDCyclicBarrierWorker(CyclicBarrier entryBarrier,
CyclicBarrier exitBarrier, ...) {
mEntryBarrier = entryBarrier; mExitBarrier = exitBarrier;
...
}
public void run() {
...
mEntryBarrier.await();
runTest();
mExitBarrier.await();
...
Exit barrier waits until all threads are done before returning
![Page 31: Java CyclicBarrier: Example Applicationschmidt/cs891s/2020-PDFs/10.4.3-CyclicB… · 2 •Understand the structure & functionality of Java CyclicBarrier •Recognize the key methods](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb493b90ec2070203242a9c/html5/thumbnails/31.jpg)
31
End of Java CyclicBarrier:Example Application