Reputation: 72646
I have a JFrame that shows a preview content, since the loading of preview data could take several times i decided to put the loading operation into a SwingWorker, here's a code sample :
public void setPreviewContent(final String content) {
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
frame.setCursor(java.awt.Cursor.getPredefinedCursor(
java.awt.Cursor.WAIT_CURSOR));
//DO My Work
return null;
}
@Override
protected void done() {
frame.setCursor(java.awt.Cursor.getPredefinedCursor(
java.awt.Cursor.DEFAULT_CURSOR));
}
};
worker.execute();
}
My frame it is initialized each time that is shown and is disposed each time that is closed with :
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
After the initialization and that is show the setPreviewContent()
method is called and it work properly, the only problem is that each time i close and reopen the preview frame a Daemon Thread is created and is left running :
As you can see soon a huge number of threads remain in running state, causing a leak. How can i resolve the problem ?
If i use a standard Thread i don't have this problem ...
Upvotes: 4
Views: 1091
Reputation: 2028
The JavaDoc says:
Schedules this SwingWorker for execution on a worker thread. There are a number of worker threads available. In the event all worker threads are busy handling other SwingWorkers this SwingWorker is placed in a waiting queue.
Did you try to close and reopen it more than 6 times, to see, whether eventually no new Thread is added? My guess is that you didn't reach the thread pool limit yet.
Upvotes: 4