在我正在开发的分布式多线程应用程序中出现了此问题,线程在分布式任务执行期间可能会随机返回。假设在此应用程序的所有线程使用相同的基本代码如下块:什么情况会导致Java线程意外返回?
public class ExecutionThread extends Thread{
@Override
public void run(){
while(true){
try{
//task submitter app dumps all Tasks in to a queue. We retrieve
//those tasks here one-by-one and run them:
Task t = priorityBlockingQueue.take();
//execute is abstract, so it could potentially do anything here:
t.execute();
} catch(Throwable e){
//task submitter app will be able to access the Throwable later:
t.setUncaughtThrowable(e);
}
}
}
}
我要指出的是,工作类是抽象的和非常简单的 - 它不仅具有execute()方法是抽象的,所以我不觉得有必要在这里发布代码。我知道在JVM中可能会发生一些疯狂的事情,但这段代码对我来说看起来相当不错,因为线程永远不会返回,除非它运行的JVM死亡。实际上,我们发现线程仍然返回,而其各自的JVM不会死于,也无法确定原因。更糟糕的是,我们无法看到这些线程的控制台,因为它们正在催生为远程计算机上的子进程。我们已经尝试记录这个throwable,但没有打印到文件。任何想法仍然可能导致此线程返回?
这里“返回”是什么意思?返回没有例外? – 2012-02-13 18:33:55
是的 - 没有任何异常被抛出,run()方法刚刚结束。 – CodeBlind 2012-02-13 18:37:32
什么是setUncaughtThrowable? – Snicolas 2012-02-13 18:40:17