|
| 1 | +package com.fd.tryout.concurrency.java; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.concurrent.CountDownLatch; |
| 5 | +import java.util.concurrent.ExecutorService; |
| 6 | +import java.util.concurrent.Executors; |
| 7 | +import java.util.stream.Collectors; |
| 8 | +import java.util.stream.Stream; |
| 9 | + |
| 10 | +/** |
| 11 | + * This is a very nice example of using CountDownLatch. In this code we generate 15 threads, start them but |
| 12 | + * block them all until all the threads get into their run method. Fire the begin sign, and wait until |
| 13 | + * all the workers finish their job. |
| 14 | + * |
| 15 | + * @author fdanismaz |
| 16 | + * date: 11/24/18 11:18 PM |
| 17 | + */ |
| 18 | +public class PoolOfThreadsWaitingToBegin { |
| 19 | + |
| 20 | + private static class Worker implements Runnable { |
| 21 | + |
| 22 | + private int id; |
| 23 | + private CountDownLatch readyThreadCounter; |
| 24 | + private CountDownLatch launcherBlocker; |
| 25 | + private CountDownLatch completedThreadCounter; |
| 26 | + |
| 27 | + |
| 28 | + public Worker(int id, CountDownLatch readyThreadCounter, |
| 29 | + CountDownLatch launcherBlocker, CountDownLatch completedThreadCounter) { |
| 30 | + this.id = id; |
| 31 | + this.readyThreadCounter = readyThreadCounter; |
| 32 | + this.launcherBlocker = launcherBlocker; |
| 33 | + this.completedThreadCounter = completedThreadCounter; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void run() { |
| 38 | + // Thread is ready. Countdown |
| 39 | + System.out.println(String.format("Worker %d is ready", this.id)); |
| 40 | + this.readyThreadCounter.countDown(); |
| 41 | + |
| 42 | + // Wait for other threads to be ready |
| 43 | + try { |
| 44 | + this.launcherBlocker.await(); |
| 45 | + } catch (InterruptedException e) { |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + |
| 49 | + // do work |
| 50 | + System.out.println(String.format("Running job %d", this.id)); |
| 51 | + try { |
| 52 | + Thread.sleep(1000); |
| 53 | + } catch (InterruptedException e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + |
| 57 | + // Thread has completed its job, countdown |
| 58 | + this.completedThreadCounter.countDown(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static CountDownLatch readyThreadCounter = new CountDownLatch(15); |
| 63 | + private static CountDownLatch launcherBlocker = new CountDownLatch(1); |
| 64 | + private static CountDownLatch completedThreadCounter = new CountDownLatch(15); |
| 65 | + |
| 66 | + public static int id = 1; |
| 67 | + |
| 68 | + public static void main(String[] args) throws InterruptedException { |
| 69 | + ExecutorService s = Executors.newCachedThreadPool(); |
| 70 | + System.out.println("Genearting threads..."); |
| 71 | + List<? extends Runnable> workerList = Stream |
| 72 | + .generate(() -> new Worker(id++, readyThreadCounter, launcherBlocker, completedThreadCounter)) |
| 73 | + .limit(15) |
| 74 | + .collect(Collectors.toList()); |
| 75 | + |
| 76 | + System.out.println("Threads are generated, waiting for them to become ready..."); |
| 77 | + workerList.forEach(worker -> s.submit(worker)); |
| 78 | + readyThreadCounter.await(); // Wait until all threads get in their run method |
| 79 | + System.out.println("All threads are ready"); |
| 80 | + |
| 81 | + // all threads are waiting for the begin sign, so let's start the threads |
| 82 | + Thread.sleep(5000); |
| 83 | + System.out.println("Firing the begin sign..."); |
| 84 | + launcherBlocker.countDown(); |
| 85 | + |
| 86 | + completedThreadCounter.await(); |
| 87 | + // all threads have completed their work |
| 88 | + System.out.println("All the workers completed their jobs"); |
| 89 | + |
| 90 | + s.shutdown(); |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments