2014-12-06 58 views
1

我對Java線程和操作系統線程有疑問,並且我讀Java Threads vs PthreadsJava Threads vs OS Threads,但我沒有找到對我的混淆的答案。創建Thread實例時發生了什麼,Java

我們在調用start()之前已經考慮過了,沒有創建OS線程。

但根據,

一個線程還沒有開始爲NEW狀態。

我錯了,或者在java doc中定義的狀態不僅僅是標記Thread實例的狀態嗎?

我的問題是:

  • 當我們創建一個線程實例,但調用start()
  • 之前到底發生了什麼時,確實在Java中

回答

2
Thread thread = new Thread(){ 
    @Override 
    public void run() { 
     // code 
    } 
}; 
// at this point the thread is in NEW state, all you have a simple java object, 
// no actual thread is created 

thread.start(); 
// when start() is invoked, at some unspecified point in the near future 
// the thread will go into RUNNABLE state, this means an actual thread will be created. 
// That can happen before start() returns. 
+0

爲什麼線程確實創造了一個操作系統線程在我們有線程之前有狀態? – Jaskey 2014-12-16 11:49:31

相關問題