From b848bfead36d50ef13f8cbec3558f4a76f520357 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Wed, 21 Jul 2021 05:17:19 +0200 Subject: [PATCH] add Phaser implementations (#11051) --- .../ThreadsStartAtSameTime.java | 27 +++++++++++++++++++ .../WorkerWithPhaser.java | 26 ++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/threadsstartatsametime/WorkerWithPhaser.java diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/threadsstartatsametime/ThreadsStartAtSameTime.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/threadsstartatsametime/ThreadsStartAtSameTime.java index 72363aeb82..3e90bd4399 100644 --- a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/threadsstartatsametime/ThreadsStartAtSameTime.java +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/threadsstartatsametime/ThreadsStartAtSameTime.java @@ -3,6 +3,7 @@ package com.baeldung.threadsstartatsametime; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.Phaser; public class ThreadsStartAtSameTime { @@ -12,6 +13,11 @@ public class ThreadsStartAtSameTime { Thread.sleep(30); usingCyclicBarrier(); + + Thread.sleep(30); + + usingPhaser(); + } private static void usingCountDownLatch() throws InterruptedException { @@ -56,4 +62,25 @@ public class ThreadsStartAtSameTime { barrier.await(); } + private static void usingPhaser() throws InterruptedException { + System.out.println("\n==============================================="); + System.out.println(" >>> Using Phaser <<<"); + System.out.println("==============================================="); + + Phaser phaser = new Phaser(); + phaser.register(); + + WorkerWithPhaser worker1 = new WorkerWithPhaser("Worker with phaser 1", phaser); + WorkerWithPhaser worker2 = new WorkerWithPhaser("Worker with phaser 2", phaser); + + worker1.start(); + worker2.start(); + + Thread.sleep(10);//simulation of some actual work + + System.out.println("-----------------------------------------------"); + System.out.println(" Now open the phaser barrier:"); + System.out.println("-----------------------------------------------"); + phaser.arriveAndAwaitAdvance(); + } } diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/threadsstartatsametime/WorkerWithPhaser.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/threadsstartatsametime/WorkerWithPhaser.java new file mode 100644 index 0000000000..44994d1cd0 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/threadsstartatsametime/WorkerWithPhaser.java @@ -0,0 +1,26 @@ +package com.baeldung.threadsstartatsametime; + +import java.time.Instant; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Phaser; + +public class WorkerWithPhaser extends Thread { + private Phaser phaser; + + public WorkerWithPhaser(String name, Phaser phaser) { + this.phaser = phaser; + phaser.register(); + setName(name); + } + + @Override public void run() { + try { + System.out.printf("[ %s ] created, blocked by the phaser\n", getName()); + phaser.arriveAndAwaitAdvance(); + System.out.printf("[ %s ] starts at: %s\n", getName(), Instant.now()); + // do actual work here... + } catch (IllegalStateException e) { + e.printStackTrace(); + } + } +}