SwingWorker threads / memory leak when creating multiple instances

I have a JFrame that shows the contents of the preview, since loading the preview data may take several times, and I decided to put the loading operation in SwingWorker, here is a sample code:

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 is initialized every time it is displayed and positioned every time it closes:

 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

After initializing and displaying the setPreviewContent() method, setPreviewContent() is called and works correctly, the only problem is that every time I close and open the preview frame again, a Daemon stream is created and it remains running:

Threads leak

As you can see shortly, a huge number of threads causing leakage remain operational. How can i solve the problem?

If I use standard Thread i, I don't have this problem ...

+4
source share
1 answer

The JavaDoc says:

Schedules this SwingWorker to run on a workflow. Several workflows are available. In the event that all workflows are busy processing other SwingWorkers, this SwingWorker is queued.

Have you tried closing and reopening it more than 6 times to see if a new topic is added as a result? I assume that you have not reached the limit of the thread pool.

+4
source

Source: https://habr.com/ru/post/1394565/


All Articles