最近,我遇到了一個或兩個Java GUI應用程序在關閉時無法關閉的問題。 它們似乎是一個過程,消耗著計算機資源。 今天,我深入探究了問題的根源,這是一個我以前從未意識到的棘手問題,所以我想我會分享一下。
理論上,當您關閉Java應用程序時,應停止所有線程,并且進程應終止。 就我而言,當我監視應用程序時,我希望完成的線程(例如Swing工作池)仍然處于活動狀態,即Strange。 原因是AWT Shutdown線程沒有終止所有幫助程序線程,原因是EventQueues中仍然存在AWT事件。 我將解釋其原因是真正的偷偷摸摸的小gatcha。
我的應用程序使用了一個具有常規睡眠的線程,但是當醒來時會進行一些計算,然后調用以更新gui:
Thread updateThread = new Thread(new Runnable() {@Override
public void run() {int i = 0;do {try {Thread.sleep(300); // 300msgui.updateValue(SOME_VALUE)} catch(InterruptException ex) {return;} frame.setValue(SOMEDATA); } while(i++ < 100); } }, "updateThread");updateThread.setDaemon(true);
updateThread.start();
現在您將注意到,如果線程被中斷并且作為守護程序線程啟動,則該線程返回。 我曾以為,作為應用程序關閉的一部分,線程將終止,但實際上不是。 這是由gui.updateValue(SOME_VALUE)使用InvokeLater引起的:
public void updateValue(final int value) {// make sure we access graphics in the EDT threadjava.awt.EventQueue.invokeLater(new Runnable() {@Override public void run() { try { ......... SOME CODE }catch(Exception t) {// not a lot to do } } });
}
InvokeLater基本上是在EventQueue上放置一個事件,因此AWT Shutdown線程想要關閉應用程序。 AWT Shutdown線程每秒鐘檢查一次EventQueues,但是正如您將看到的,我的Thread會執行更新一秒(300ms),因此隊列上始終有一個事件! 簡而言之,AWT Shutdown線程永遠不會終止我希望其終止的線程,因此需要終止應用程序。
在我的線程的while循環中,變通方法很簡單,我還檢查了通過它進行更新的JComonent是否仍然可見并顯示,如果不是退出循環,則該線程死亡,因此沒有其他事件放在事件線程上,應用程序按預期關閉:)
Thread updateThread = new Thread(new Runnable() {@Override
public void run() {int i = 0; do { try { Thread.sleep(300); // 300msgui.updateValue(SOME_VALUE); }catch(InterruptException ex) { return;} frame.setValue(SOMEDATA);}while(i < 100 && progressGlassPane.isVisible() && progressGlassPane.isShowing());}
}, "updateThread");
updateThread.setDaemon(true);
updateThread.start();
因此,簡而言之,不要以低于一秒的頻率從幫助程序線程中調用InvokeLater,除非在正在更新的組件不再可見的情況下也終止了該線程!
作為旁注,發現問題后,我發現這非常
參考: Coal Face博客上Java桌面開發的 JCG合作伙伴 Steve Webb的Java GUI Application Shutdown Gotcha 。
翻譯自: https://www.javacodegeeks.com/2012/05/java-gui-application-shutdown-gotcha.html