2015-01-26 30 views
0

下面的線程類工作正常。我可以理解它的過程。然後,我改變會發生什麼使用run()而不是線程的start()?

mc.srart()mc.run(),但什麼都沒有改變,也沒有任何錯誤。

有人可以向我解釋這個嗎?我們總是可以用run()而不是start()

public class Main { 

    public static void main(String[] args) { 

     Myclass mc = new Myclass(); 
     mc.start(); 
    } 
} 

class Myclass extends Thread { 
    public void run() { 
     for (int i = 0; i < 10; i++) { 
      System.out.print(i + "--"); 
     } 
    } 
} 
+0

't.start()'是庫提供給您的代碼調用以啓動新線程的方法。 'run()'是你的代碼提供給庫調用新線程的方法。 'run()'方法是定義線程將執行什麼的方法。 – 2015-01-26 18:50:09

回答

5

調用run直接在Thread對象上擊敗在首位具有Thread的點。

如果您撥打run,那麼run將作爲正常方法在當前Thread中執行。必須在Thread上撥打the startmethod才能使run在另一個Thread中執行。

導致此線程開始執行; Java虛擬機會調用此線程的運行方法。

相關問題