Main.java 1.12 KB
Newer Older
Gabriele Civitarese's avatar
Gabriele Civitarese committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package countdownlatches;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {

    public static void main(String[] args) {

        int numberOfThreads = 5;

        //creating an executor with a fixed number of threads
        ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);

        //creating a countdown which has exactly the number of threads we want to launch
        CountDownLatch countDownLatch = new CountDownLatch(numberOfThreads);

        //let's start all the threads with the executor
        for(int i = 0; i < numberOfThreads; i++){

            executorService.submit(new Worker(countDownLatch));

        }

        System.out.println("Waiting for thread termination...");

        try {
            //blocking until all the threads executed countDown()
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("All the threads concluded their work.");

        executorService.shutdown();
    }


}