fork download
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.concurrent.CountDownLatch;
  4.  
  5. class Ideone {
  6. private static final CountDownLatch countDownLatch = new CountDownLatch(2);
  7. private static volatile boolean flag = false;
  8.  
  9. public static void main(String[] args) throws InterruptedException {
  10. List<Runnable> runnables = Arrays.asList(() -> {
  11. try {
  12. while (true) {
  13. Thread.sleep(100);
  14. if (flag) {
  15. countDownLatch.countDown();
  16. System.out.println("Second Function");
  17. break;
  18. }
  19. }
  20. } catch (Exception e) {
  21. }
  22. }, () -> {
  23. try {
  24. Thread.sleep(100);
  25. System.out.println("First Function");
  26. flag = true;
  27. countDownLatch.countDown();
  28. System.out.println("End of fast function");
  29. System.out.println(flag);
  30. } catch (Exception e) {
  31. }
  32. });
  33. executeAndWait(runnables);
  34. countDownLatch.await();
  35. System.out.println("Main Execution Completed!");
  36. }
  37.  
  38. private static void executeAndWait(List<Runnable> runnables) {
  39. runnables.forEach(r -> new Thread(r).start());
  40. }
  41. }
Success #stdin #stdout 0.1s 50132KB
stdin
Standard input is empty
stdout
First Function
End of fast function
true
Second Function
Main Execution Completed!