2011-01-26 56 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

相关问题