Created
June 16, 2017 15:25
-
-
Save Normal/c26504c38c37f3a21cb8ef3335a8357c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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