2011-01-26 55 views
1

爲什麼主線程永遠不會執行?我認爲這是我使用Thread.sleep(int value)我給了其他線程運行的機會,但這從來沒有發生。一個線程始終運行,沒有其他線程的機會

public static void main(String[] args) { 
     final Sook o = new Sook(); 
     Thread t = new Thread(new Runnable() { 
      public void run() { 
       while (true) { 
        try { 
         Thread.sleep(10000); // Specially set to give a chance to the main thread to run 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }); 

     t.run(); 

     System.out.println("<<<<<BACK TO MAIN >>>>>>"); // Never happens 
    } 

回答

12

不要叫,t.run()調用t.start()

只要運行將調用run方法在當前線程。

+1

此外,你不能吞下`InterruptedException`。在catch塊中應該有一個`return;`。 – biziclop 2011-01-26 18:35:34

相關問題