| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,81 @@ | |||
| 1 | + package com.brianway.learning.java.concurrent; | ||
| 2 | + | ||
| 3 | + import java.util.Random; | ||
| 4 | + import java.util.concurrent.CountDownLatch; | ||
| 5 | + import java.util.concurrent.ExecutorService; | ||
| 6 | + import java.util.concurrent.Executors; | ||
| 7 | + import java.util.concurrent.TimeUnit; | ||
| 8 | + | ||
| 9 | + public class CountDownLatchDemo { | ||
| 10 | + static final int SIZE = 10; | ||
| 11 | + | ||
| 12 | + public static void main(String[] args) throws Exception { | ||
| 13 | + ExecutorService exec = Executors.newCachedThreadPool(); | ||
| 14 | + // All must share a single CountDownLatch object: | ||
| 15 | + CountDownLatch latch = new CountDownLatch(SIZE); | ||
| 16 | + for (int i = 0; i < 10; i++) | ||
| 17 | + exec.execute(new WaitingTask(latch)); | ||
| 18 | + for (int i = 0; i < SIZE; i++) | ||
| 19 | + exec.execute(new TaskPortion(latch)); | ||
| 20 | + System.out.println("Launched all tasks"); | ||
| 21 | + exec.shutdown(); // Quit when all tasks complete | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + // Performs some portion of a task: | ||
| 26 | + class TaskPortion implements Runnable { | ||
| 27 | + private static int counter = 0; | ||
| 28 | + private final int id = counter++; | ||
| 29 | + private static Random rand = new Random(47); | ||
| 30 | + private final CountDownLatch latch; | ||
| 31 | + | ||
| 32 | + TaskPortion(CountDownLatch latch) { | ||
| 33 | + this.latch = latch; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public void run() { | ||
| 37 | + try { | ||
| 38 | + doWork(); | ||
| 39 | + latch.countDown(); | ||
| 40 | + latch.await(); | ||
| 41 | + System.out.println(this + " TaskPortion after await"); | ||
| 42 | + } catch (InterruptedException ex) { | ||
| 43 | + // Acceptable way to exit | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public void doWork() throws InterruptedException { | ||
| 48 | + TimeUnit.MILLISECONDS.sleep(rand.nextInt(2000)); | ||
| 49 | + System.out.println(this + "completed"); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public String toString() { | ||
| 53 | + return String.format("%1$-3d ", id); | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + // Waits on the CountDownLatch: | ||
| 58 | + class WaitingTask implements Runnable { | ||
| 59 | + private static int counter = 0; | ||
| 60 | + private final int id = counter++; | ||
| 61 | + private final CountDownLatch latch; | ||
| 62 | + | ||
| 63 | + WaitingTask(CountDownLatch latch) { | ||
| 64 | + this.latch = latch; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public void run() { | ||
| 68 | + try { | ||
| 69 | + latch.await(); | ||
| 70 | + System.out.println("Latch barrier passed for " + this); | ||
| 71 | + } catch (InterruptedException ex) { | ||
| 72 | + System.out.println(this + " interrupted"); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + public String toString() { | ||
| 77 | + return String.format("WaitingTask %1$-3d ", id); | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + /* (Execute to see output) *///:~ | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments