Commit 7c059f32 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Creating an example with TimeUnit

parent e35cce2d
Loading
Loading
Loading
Loading
+20 −7
Changes for src/main/java/ch15/PredictableRunOrder.java: 20 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -14,18 +14,31 @@ class PredictableSleep {
  public static void main (String[] args) throws InterruptedException {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> sleepThenPrint());
//    Thread thread = new Thread(() -> sleepThenPrint());
//    thread.start();
//    Thread.sleep(1000L);
//    System.out.println(thread.getState());
    System.out.println("back in main");
//    executor.awaitTermination(1100L, TimeUnit.MILLISECONDS);
//    executor.shutdown();
    executor.shutdown();
  }

  private static void sleepThenPrint() {
    try {
      Thread.sleep(10_000L);
      Thread.sleep(120000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("top o’ the stack");
  }
}

class SleepWithTimeUnit {
  public static void main (String[] args) throws InterruptedException {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> sleepThenPrint());
    System.out.println("back in main");
    executor.shutdown();
  }

  private static void sleepThenPrint() {
    try {
      TimeUnit.MINUTES.sleep(2);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }