Java GUI Application Shutdown Gotcha

In recent times I’ve had issues with one or two Java GUI application not shutting down when I close them. They seem to stay around as a process, consuming computer resources. Today I got to the bottom of the problem and it’s a bit of a nasty gotcha which I wasn’t aware of before so I thought I would share it.
In theory when you close a Java application all the threads should be stopped and the process should die. Im my case when I monitored the application the threads I expected to finish such as Swing worker pools were still alive, Strange. The reason turned out to be that the AWT Shutdown thread wasn’t terminating all the helper threads, and the reason for this was that there were still AWT Events in the EventQueues. The reason for this is a real sneaky little gatcha, I will explain.
My application used a Thread which had a regular sleep but when woke up would so some calculation and then make a call to update the gui:
Thread updateThread = new Thread(new Runnable() {

@Override
public void run() {
 int i = 0;
 do {
   try {
    Thread.sleep(300); // 300ms
    gui.updateValue(SOME_VALUE)
    } catch(InterruptException ex) {
      return;
    } 
    frame.setValue(SOMEDATA);  
   } while(i++ < 100); 
  }    
             
}, "updateThread");

updateThread.setDaemon(true);
updateThread.start();
Now you will notice that the thread returns if it is interrupted and also it is started as a Daemon thread. I had thought that as part of the application shutdown the thread would be terminated but NO it wasn’t. This was caused by gui.updateValue(SOME_VALUE) making use of InvokeLater:
public void updateValue(final int value) {

   // make sure we access graphics in the EDT thread
   java.awt.EventQueue.invokeLater(new Runnable() {

    @Override          
     public void run() {       
       try {   
         ...
         ...
         ...        
         SOME CODE 
       }catch(Exception t) {
        // not a lot to do    
       } 
     }    
  });
}
The InvokeLater is basically putting an event on the EventQueue and because of this the AWT Shutdown thread want shutdown the application. The AWT Shutdown thread checks the EventQueues every seconds but as you will see my Thread does an update subsecond (300ms) so there is always an event on the Queue! So in short the AWT Shutdown thread never terminates the threads I want it to terminate and so the application needs to be killed.
The work around is simple in the while loop of my thread I also check that the JComonent that is to be updated via it is still visible and shown, if it is not the loop is exited, the thread dies and so there are no more events put on the Event thread and whooohooo the application closes as expected :)
Thread updateThread = new Thread(new Runnable() {

@Override
public void run() {
 int i = 0;     
 do {   
  try { 
   Thread.sleep(300); // 300ms
   gui.updateValue(SOME_VALUE); 
   }catch(InterruptException ex) {  
    return;
   }   
   frame.setValue(SOMEDATA);
  }while(i < 100 && progressGlassPane.isVisible() && progressGlassPane.isShowing());
 }
}, "updateThread");
updateThread.setDaemon(true);
updateThread.start();
So in short don’t call InvokeLater from a helper thread at sub-second frequency unless you also terminate the thread if the component it is updating is no longer visible!
As a side note after I spotted the issue I found this very

Reference: Java GUI Application Shutdown Gotcha from our JCG partner Steve Webb at the Java Desktop Development at the Coal Face blog.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Leave a Reply


× three = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books