我試圖關閉應用程序,每當有任何致命的 錯誤/異常來臨,但在關閉應用程序之前,我當前的 線程/任務應該完成,所以我在run()
內寫入mainThread.join()
,無異常時工作正常。但是每當我的 doTask()
拋出異常那個時候join()
永遠等待。join()在發生異常時永久等待,jvm關機掛鉤不工作
public class POC
{
public void doTask() throws Exception
{
throw new Exception("Fatal Error");
//throw new Exception("Fatal Error"); By commenting working fine.
}
public static void main(String[] args)
{
POC ob = new POC();
final Thread mainThread = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
try
{
System.out.println("Join() Start");
mainThread.join();
System.out.println("Join() End"); //Why this is not printing?
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});
try
{
System.out.println("Before doTask()");
ob.doTask(); //User Defined Run()
System.out.println("After doTask()");
}
catch (Exception ex) // FATAL
{
System.err.println("Exception : " + ex.getLocalizedMessage());
System.exit(-1);
}
}
}
OutPut : 0
Before Run()
Exception : Fatal Error
Join() Start
爲什麼System.out.println("Join() End");
不打印?
>請讓我知道,有沒有解決方案來釋放僵局並強行關閉jvm。 –