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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


5 × four =



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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close