在我正在開發的分佈式多線程應用程序中出現了此問題,線程在分佈式任務執行期間可能會隨機返回。假設在此應用程序的所有線程使用相同的基本代碼如下塊:什麼情況會導致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