Skip to content

Instantly share code, notes, and snippets.

@Normal
Created June 16, 2017 15:25
Show Gist options
  • Save Normal/c26504c38c37f3a21cb8ef3335a8357c to your computer and use it in GitHub Desktop.
Save Normal/c26504c38c37f3a21cb8ef3335a8357c to your computer and use it in GitHub Desktop.
package com.example.demo;
import java.util.concurrent.CountDownLatch;
public class CountDownLatchExample {
private static final CountDownLatch START = new CountDownLatch(5);
public static void main(String[] args) throws InterruptedException {
System.out.println("Waiting 5 cars appearing to the start line");
for (int i = 1; i <= 5; i++) {
new Car(i).start();
}
START.await();
System.out.println("START!");
}
static class Car extends Thread {
private int number;
Car(int number) {
this.number = number;
}
@Override
public void run() {
try {
Thread.sleep(number * 1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(number + " car is ready to start");
START.countDown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment