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 ...