Created
August 6, 2018 16:26
-
-
Save WayneCui/8199b7cd0f95342dc5448c4b5df9fca1 to your computer and use it in GitHub Desktop.
CountDownLatchDemo
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
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