我正在使用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)循环”可以死吗?
Q2。如果能够死亡,如何重新运行?
谢谢你。
最佳答案
如果从while循环中断,则线程将进入状态TERMINATED,因为它将到达函数结尾。
A1。你的while循环将死
A2。你不能。您应该再次将可运行对象提交给executorService。
关于java - Java-ExecutorService : How to re run “wait state” thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34892381/