Guest User

Untitled

a guest
Dec 15th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package javacore.threading;
  2.  
  3. import java.util.concurrent.CountDownLatch;
  4.  
  5. public class CountdownlatchExample {
  6.  
  7. public static class Service implements Runnable{
  8.  
  9. private String serviceName;
  10. private long timeToStart;
  11. private CountDownLatch latch;
  12.  
  13. public Service(String serviceName, long timeToStart, CountDownLatch latch){
  14. this.serviceName = serviceName;
  15. this.timeToStart = timeToStart;
  16. this.latch = latch;
  17. }
  18.  
  19. @Override
  20. public void run() {
  21. try{
  22. Thread.sleep(234);
  23. }catch(InterruptedException ex){
  24. System.out.println();
  25. }
  26. System.out.println( this.serviceName + " is UP Now!");
  27. latch.countDown();
  28. }
  29.  
  30. }
  31.  
  32. public static void main(String[] args) {
  33. final CountDownLatch latch = new CountDownLatch(4);
  34. Thread s1 = new Thread(new Service("Service1", 1000, latch));
  35. Thread s2 = new Thread(new Service("Service2", 1000, latch));
  36. Thread s3 = new Thread(new Service("Service3", 1000, latch));
  37. Thread s4 = new Thread(new Service("Service4", 1000, latch));
  38.  
  39. s1.start();
  40. s2.start();
  41. s3.start();
  42. s4.start();
  43.  
  44. try{
  45. latch.await();
  46. System.out.println("All Service are UP and Running!");
  47. }catch(InterruptedException ex){
  48. ex.printStackTrace();
  49. }
  50.  
  51. }
  52. }
Add Comment
Please, Sign In to add comment