tdudzik

Executor service, count down latch

Sep 9th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package io.github.tdudzik.testproject;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.concurrent.CountDownLatch;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8.  
  9. class Test {
  10.  
  11. //    // Nie kończy się
  12. //    public void test() {
  13. //        ExecutorService executorService = Executors.newFixedThreadPool(5);
  14. //        IntStream.range(0, 5).forEach(i -> {
  15. //            executorService.execute(() -> {
  16. //                try {
  17. //                    Thread.sleep(1000);
  18. //                    System.out.println(i);
  19. //                } catch (InterruptedException e) {
  20. //                    e.printStackTrace();
  21. //                }
  22. //            });
  23. //        });
  24. //    }
  25.  
  26. //    // Myślałem, że nie wypisze tych cyfr ale jednak wypisuje
  27. //    // Teraz problemem jest to, że "THE END" jest na początku a MUSI być na końcu
  28. //    public void test() {
  29. //        ExecutorService executorService = Executors.newFixedThreadPool(5);
  30. //
  31. //        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
  32. //        numbers.stream().forEach(i -> {
  33. //            executorService.execute(() -> {
  34. //                try {
  35. //                    Thread.sleep(5000);
  36. //                    System.out.println(i);
  37. //                } catch (InterruptedException e) {
  38. //                    e.printStackTrace();
  39. //                }
  40. //            });
  41. //        });
  42. //
  43. //        System.out.println("THE END");
  44. //        executorService.shutdown();
  45. //    }
  46.  
  47.     // Teraz jest ok!!!
  48.     public void test() {
  49.         int n = 5;
  50.  
  51.         ExecutorService executorService = Executors.newFixedThreadPool(n);
  52.         CountDownLatch countDownLatch = new CountDownLatch(n);
  53.  
  54.         List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
  55.         numbers.stream().forEach(i -> {
  56.             executorService.execute(() -> {
  57.                 try {
  58.                     Thread.sleep(5000);
  59.                     System.out.println(i);
  60.                     countDownLatch.countDown();
  61.                 } catch (InterruptedException e) {
  62.                     e.printStackTrace();
  63.                 }
  64.             });
  65.         });
  66.  
  67.         try {
  68.             countDownLatch.await();
  69.             System.out.println("THE END");
  70.             executorService.shutdown();
  71.         } catch (InterruptedException e) {
  72.             e.printStackTrace();
  73.         }
  74.     }
  75.  
  76. }
  77.  
  78. public class TestProjectApp {
  79.  
  80.     public static void main(String[] args) {
  81.         Test test = new Test();
  82.         test.test();
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment