Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do my SwingWorker threads keep running even though they are done executing?
    primarykey
    data
    text
    <p>I'm implementing a GUI for a console application, and I need to do some actions (for example, parse an XML file) in a specified time interval. I decided to use javax.swing.Timer alongside SwingWorker to be sure that these actions will not make my application unresponsive.</p> <p>I had implemented the timer this way:</p> <pre><code>public class DataUpdateTimer extends Timer { private String dataFlowControllerXML = null; private DataUpdateWorker dataUpdateWorker = null; public class DataUpdateWorker extends SwingWorker { private String dataFlowControllerXML = null; DataUpdateWorker(String dataFlowControllerXML) { super(); this.dataFlowControllerXML = dataFlowControllerXML; } @Override protected Boolean doInBackground() throws Exception { Thread.sleep(300); return Boolean.TRUE; } } public class DataUpdateIntervalListener implements ActionListener { public void actionPerformed(ActionEvent e) { DataUpdateTimer timer = (DataUpdateTimer)e.getSource(); DataUpdateWorker dataUpdateWorker = timer.getDataUpdateWorker(); if (dataUpdateWorker != null) if (dataUpdateWorker.isDone()) { Boolean updateResult = Boolean.FALSE; try { updateResult = (Boolean)dataUpdateWorker.get(); } catch (InterruptedException ex) { } catch (ExecutionException ex) { } dataUpdateWorker = null; } // Creating new worker thread here if (dataUpdateWorker == null) { timer.dataUpdateWorker = new DataUpdateWorker(timer.dataFlowControllerXML); // Starting a new worker thread for parsing Data Flow Controller's XML timer.dataUpdateWorker.execute(); return; } } } DataUpdateTimer(Integer dataUpdateInterval, String dataFlowControllerXML) { super(dataUpdateInterval.intValue(), null); this.dataFlowControllerXML = dataFlowControllerXML; addActionListener(new DataUpdateIntervalListener()); } @Override public void stop() { super.stop(); if (dataUpdateWorker != null) { if (!dataUpdateWorker.isDone() || !dataUpdateWorker.isCancelled()) dataUpdateWorker.cancel(true); } } } </code></pre> <p>...and use it as follows:</p> <pre><code>new DataUpdateTimer(1000, dataFlowControllerXML).start(); </code></pre> <p>Everything works as I wish. Timer creates new a SwingWorker instance and executes it. After the worker is done, the new one is created and executed.</p> <p>The thing I'm confused by is that after the worker's thread is done I still can see it running in Netbeans' debugging window (for example, as SwingWorker-pool-3-thread-1) or in Windows Task Manager (the number of running threads doesn't decrease after the thread is done). The number of SwingWorker threads is limited to 10, but having them running embarrasses me.</p> <p>In the case of simple thread usage:</p> <pre><code>Thread th = new Thread(new Runnable() { public void run() { int a = 0; } }); th.start(); </code></pre> <p>This thread automatically disappears after execution.</p> <p>Is this SwingWorker behavior normal?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload