I have a statusbar in a Swing application that continuously prints status. All objects and classes within the application will be using this setStatus method to paint the status bar
public def setStatus(statusText){
Thread.start {
ljaStatusBarButton.setText("Status : $statusText . . . .")
sleep(3000)
ljaStatusBarButton.setText("Status : Waiting for user action . . . .")
interrupt() // or stop() ?
}
}
The status bar will display the status for 3 sec and revert back to a status saying waiting for user action.
This does work correctly but my concern is that the above method will be call multiple times from the UI which also means a new Thread object will be created everytime a status is set. Keeping this is mind, I’ve explicitly added a interrupt() at the end as I want to indicate the compiler that I am not in need of this thread anymore. And also, how much can I bet on Java’s garbage collection to ensure that the stopped/interrupted threads will be cleaned up soon? Or is there a better workaround for this multiple thread objects issue?
Your code is incorrect as you are using Swing outside of the EDT
As you are using groovy already, why not use the SwingBuilder helpers that were added to make this sort of thing easier?
Taken in part from the MultiThreading with SwingBuilder example