Skip to content

Instantly share code, notes, and snippets.

@WayneCui
Created August 6, 2018 16:26
Show Gist options
  • Save WayneCui/8199b7cd0f95342dc5448c4b5df9fca1 to your computer and use it in GitHub Desktop.
Save WayneCui/8199b7cd0f95342dc5448c4b5df9fca1 to your computer and use it in GitHub Desktop.
CountDownLatchDemo
import java.util.concurrent.CountDownLatch;
public class CountDownLatchDemo {
public static void main(String[] args) {
final CountDownLatch gate = new CountDownLatch(3);
Thread t1 = new Thread(){
public void run() {
try {
Thread.sleep(3000);
System.out.println("Redis is ready...");
} catch (InterruptedException ignores) {
//
}finally {
gate.countDown();
}
}
};
t1.start();
Thread t2 = new Thread() {
public void run() {
try {
Thread.sleep(2000);
System.out.println("Mysql is ready...");
} catch (InterruptedException ignores){
//
} finally {
gate.countDown();
}
}
};
t2.start();
Thread t3 = new Thread() {
public void run() {
try {
Thread.sleep(1000);
System.out.println("RabbitMQ is ready...");
} catch (InterruptedException ignores){
} finally {
gate.countDown();
}
}
};
t3.start();
try {
gate.await();
} catch (InterruptedException ignores) { }
System.out.println("All resources is ready!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment