2016-09-22T17:18:12.745 User -> RUNNABLE 2016-09-22T17:18:13.747 User -> WAITING 2016-09-22T17:18:14.729 User -> WAITING 2016-09-22T17:18:15.712 User -> WAITING 2016-09-22T17:18:16.713 User -> WAITING
Thread carThread = new Thread(() -> { System.out.println(LocalDateTime.now() + " Car run"); long now = System.currentTimeMillis(); while (true) { if (System.currentTimeMillis() - now > 3000) { break; } } System.out.println(LocalDateTime.now() + " Car Stop"); }, "car");
Thread dogThread = new Thread(() -> { System.out.println(LocalDateTime.now() + " Dog run"); long now = System.currentTimeMillis(); while (true) { if (System.currentTimeMillis() - now > 3000) { break; } } System.out.println(LocalDateTime.now() + " Dog Stop"); }, "dog");
Begin join 2016-09-22T17:17:04.053 Car run 2016-09-22T17:17:04.053 Dog run 2016-09-22T17:17:05.011 car -> RUNNABLE 2016-09-22T17:17:05.011 main -> WAITING 2016-09-22T17:17:05.011 dog -> RUNNABLE 2016-09-22T17:17:06.013 car -> RUNNABLE 2016-09-22T17:17:06.013 main -> WAITING 2016-09-22T17:17:06.013 dog -> RUNNABLE 2016-09-22T17:17:07.011 car -> RUNNABLE 2016-09-22T17:17:07.011 main -> WAITING 2016-09-22T17:17:07.011 dog -> RUNNABLE 2016-09-22T17:17:07.055 Car Stop Started join 2016-09-22T17:17:07.060 Dog Stop
当我调用carThread.join(3);结果为
1 2 3 4 5 6 7 8 9 10 11 12
Begin join Started join 2016-09-22T17:19:26.029 Car run 2016-09-22T17:19:26.036 Dog run 2016-09-22T17:19:27.004 dog -> RUNNABLE 2016-09-22T17:19:27.005 car -> RUNNABLE 2016-09-22T17:19:28.004 dog -> RUNNABLE 2016-09-22T17:19:28.004 car -> RUNNABLE 2016-09-22T17:19:29.005 dog -> RUNNABLE 2016-09-22T17:19:29.005 car -> RUNNABLE 2016-09-22T17:19:29.030 Car Stop 2016-09-22T17:19:29.037 Dog Stop
2016-09-22T17:29:12.378 User -> RUNNABLE 2016-09-22T17:29:13.345 User -> RUNNABLE User Sleep 2016-09-22T17:29:14.346 User -> TIMED_WAITING 2016-09-22T17:29:15.347 User -> TIMED_WAITING User Sleep Finshed
Thread carThread = new Thread(() -> { System.out.println(LocalDateTime.now() + " Car run"); long now = System.currentTimeMillis(); while (true) { if (System.currentTimeMillis() - now > 3000) { break; } } System.out.println(LocalDateTime.now() + " Car Stop"); }, "car");
Thread dogThread = new Thread(() -> { System.out.println(LocalDateTime.now() + " Dog run"); long now = System.currentTimeMillis(); while (true) { if (System.currentTimeMillis() - now > 3000) { break; } } System.out.println(LocalDateTime.now() + " Dog Stop"); }, "dog");
Begin join 2016-09-22T18:23:56.591 Dog run 2016-09-22T18:23:56.601 Car run 2016-09-22T18:23:57.646 main -> TIMED_WAITING 2016-09-22T18:23:57.647 dog -> RUNNABLE 2016-09-22T18:23:57.647 car -> RUNNABLE 2016-09-22T18:23:58.582 main -> TIMED_WAITING 2016-09-22T18:23:58.582 dog -> RUNNABLE 2016-09-22T18:23:58.582 car -> RUNNABLE end join 2016-09-22T18:23:59.583 dog -> RUNNABLE 2016-09-22T18:23:59.583 car -> RUNNABLE 2016-09-22T18:23:59.592 Dog Stop 2016-09-22T18:23:59.602 Car Stop