import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;

class Ideone {
  private static final CountDownLatch countDownLatch = new CountDownLatch(2);
  private static volatile boolean flag = false;

  public static void main(String[] args) throws InterruptedException {
    List<Runnable> runnables = Arrays.asList(() -> {
      try {
        while (true) {
          Thread.sleep(100);
          if (flag) {
            countDownLatch.countDown();
            System.out.println("Second Function");
            break;
          }
        }
      } catch (Exception e) {
      }
    }, () -> {
      try {
        Thread.sleep(100);
        System.out.println("First Function");
        flag = true;
        countDownLatch.countDown();
        System.out.println("End of fast function");
        System.out.println(flag);
      } catch (Exception e) {
      }
    });
    executeAndWait(runnables);
    countDownLatch.await();
    System.out.println("Main Execution Completed!");
  }

  private static void executeAndWait(List<Runnable> runnables) {
    runnables.forEach(r -> new Thread(r).start());
  }
}