Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javacore.threading;
- import java.util.concurrent.CountDownLatch;
- public class CountdownlatchExample {
- public static class Service implements Runnable{
- private String serviceName;
- private long timeToStart;
- private CountDownLatch latch;
- public Service(String serviceName, long timeToStart, CountDownLatch latch){
- this.serviceName = serviceName;
- this.timeToStart = timeToStart;
- this.latch = latch;
- }
- @Override
- public void run() {
- try{
- Thread.sleep(234);
- }catch(InterruptedException ex){
- System.out.println();
- }
- System.out.println( this.serviceName + " is UP Now!");
- latch.countDown();
- }
- }
- public static void main(String[] args) {
- final CountDownLatch latch = new CountDownLatch(4);
- Thread s1 = new Thread(new Service("Service1", 1000, latch));
- Thread s2 = new Thread(new Service("Service2", 1000, latch));
- Thread s3 = new Thread(new Service("Service3", 1000, latch));
- Thread s4 = new Thread(new Service("Service4", 1000, latch));
- s1.start();
- s2.start();
- s3.start();
- s4.start();
- try{
- latch.await();
- System.out.println("All Service are UP and Running!");
- }catch(InterruptedException ex){
- ex.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment