Home » Java » Desktop Java » Java GUI Application Shutdown Gotcha

About Steve Webb

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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


5 × nine =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close