2016-01-20 58 views
2

我使用的ExecutorService喜欢,爪哇 - ExecutorService的:如何重新运行 “等待状态” 线程

ExecutorService executorService = Executors.newFixedThreadPool(5); 

for(int i = 0 ; i < 5 ; i ++){ 
    executorService.submit(new MyRunnable()); 
} 

和我的Runnable像,

@Override 
public void run(){ 
    while(true){ 
     try{ 
      Data data = SynchronizedQueue.getData(); // Class Name is Sample... 
      if(data != null){ 
       //process with data 
      }else{ 
       Thread.sleep(1); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

当我从Runnable接口的while-打破循环,线程的状态改变为“等待”。

Q1。我的“while(true)loop”会死吗? Q2302。如果它能够死亡,如何重新运行?

谢谢。

回答

0

如果你从while循环中断,你的线程将进入状态TERMINATED状态,因为它会到达函数的结尾。

A1。你的while循环会死亡

A2。你不能。你应该再次将你的runnable提交给executorService。

+0

编号VisualVM告诉我线程的状态是“等待”。 – bot

+0

是否等于等于终止? – bot

+0

我以为Executor.newFixedThreadPool保持线程 – bot