2016-09-22 89 views
0

按的Javadoc Thread.State.TIMED_WAITING,我寫代碼:爲什麼調用連接方法時主線程不見了?

import java.time.LocalDateTime; 
import java.util.concurrent.TimeUnit; 

public class TestJoin { 

public static void main(String[] args) throws InterruptedException { 
    ThreadUtil.printThreadState("main", "car", "dog"); 

    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"); 

    try { 
     dogThread.start(); 
     carThread.start(); 

     System.out.println("Begin join"); 
     carThread.join(3); 
     System.out.println("endjoin"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
}} 

import java.time.LocalDateTime; 

public class ThreadUtil { 

    public static void printThreadState(String... filter) { 
     Thread print = new Thread(() -> { 
      long now = System.currentTimeMillis(); 
      while (true) { 
       if (System.currentTimeMillis() - now > 1000) { 
        now = System.currentTimeMillis(); 
        Thread.getAllStackTraces().forEach((key, thread) -> { 
         for (int i = 0; i < filter.length; i++) { 
          if (key.getName().equals(filter[i])) { 
           System.out.println(LocalDateTime.now() + " " +key.getName() + " -> " + key.getState()); 
          } 
         } 
        }); 
       } 
      } 
     }, "Print"); 
     print.start(); 
    } 
} 

輸出

Begin join 
end join 
2016-09-22T18:01:53.484 Car run 
2016-09-22T18:01:53.498 Dog run 
2016-09-22T18:01:54.460 dog -> RUNNABLE 
2016-09-22T18:01:54.460 car -> RUNNABLE 
2016-09-22T18:01:55.531 dog -> RUNNABLE 
2016-09-22T18:01:55.532 car -> RUNNABLE 
2016-09-22T18:01:56.461 dog -> RUNNABLE 
2016-09-22T18:01:56.462 car -> RUNNABLE 
2016-09-22T18:01:56.486 Car Stop 
2016-09-22T18:01:56.499 Dog Stop 

像那個文件夾,主線程steat是 'TIMED_WAITING' ,但它消失了,主要的東西在哪裏?

PS: 當我寫

carThread.join(); 

看來

Begin join 
2016-09-22T18:04:31.583 Dog run 
2016-09-22T18:04:31.584 Car run 
2016-09-22T18:04:32.543 main -> WAITING 
2016-09-22T18:04:32.543 dog -> RUNNABLE 
2016-09-22T18:04:32.543 car -> RUNNABLE 
2016-09-22T18:04:33.604 main -> WAITING 
2016-09-22T18:04:33.604 dog -> RUNNABLE 
2016-09-22T18:04:33.604 car -> RUNNABLE 
2016-09-22T18:04:34.585 Car Stop 
end join 
2016-09-22T18:04:34.608 Dog Stop 
2016-09-22T18:04:34.608 dog -> TERMINATED 

回答

2

因爲沒有join其他線程之前,你的主線程完成。 join使主線程「等待」,直到完成調用join的線程爲止,直到此時爲止。

+0

無論主線程使用的等待超時如何,在所有這些線程完成之前,JVM都不會終止。 –

+0

@AndrewLygin誤導錯誤刪除。 – Antoniossss

相關問題