当父函数终止时,java不会导致内线程死亡
我的问题不清楚,写得不好,很抱歉。我会试着再试一次
所以,我有一个由另一个程序填充的队列,直到它收到停止信号。我不知道什么时候停止,所以我想做的是 在线程中,处理队列的所有元素,直到队列为空,并打印有关其元素的一些信息。 我想看看它是否在另一个类中完成了处理。能够检查它是否完成了操作 我有一个单例类,它用一些字符串信息映射未来的。这是代码
public void complete(){
ExecutorService executor = Executors.newSingleThreadExecutor();
class specCallable implements Callable<Boolean>{
boolean isOK = true;
@Override
public Boolean call() throws Exception {
//while queue is not empty get its elements and print some information about them
}
return isOK;
}
specCallable specRun = new specCallable();
Future<Boolean> future = executor.submit(specRun);
//I will put that future in my map to get it in another class and check if it is done
FutureMap futureMap = FutureMap.getInstance();
futureMap.addFuture(future, "FFT");
}
所以,在另一节课上,我从地图上取下我的未来,等待它完成
public void finishOperation() throws InterruptedException, ExecutionException{
FutureMap futureMap = FutureMap.getInstance();
Future f = futureMap.getFuture("FFT");
if(f!=null){
while(!f.isDone()){
System.out.println("Thread still alive");
Thread.sleep(100);
}
}
}
在这里,我看到未来还没有完成它的工作,它还要运行2-3秒,但我的问题是,即使未来是活跃的, 在我的日志中,我看不到“线程仍处于活动状态”日志之间的任何内容。我所期望的是,因为未来还没有结束, 它应该处理更多的队列元素并打印关于它们的信息。我怀疑这是个错误,因为我看到了 队列填充程序向队列发送的元素比我在日志中找到的多。我是不是遗漏了什么
# 1 楼答案
不,它不会死的。如果要等待线程完成执行,可以使用Callable