java cyclicbarrier: example applicationschmidt/cs891s/2020-pdfs/10.4.3-cyclicb… · 2...

31
Java CyclicBarrier: Example Application Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 14-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

31

End of Java CyclicBarrier:Example Application