Java – why does my swingworker thread continue to run until execution is complete?

I am implementing the GUI for the console application, and I need to perform some operations (for example, parsing XML files) within the specified time interval I decided to use javax swing. Timer works with swingworker to ensure that these operations do not make my application unresponsive

I implemented the timer in this way:

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);
        }
    }
}

... and use the following:

new DataUpdateTimer(1000,dataFlowControllerXML).start();

Everything is in line with my wishes Timer creates a new instance of swingworker and executes it After completing the work, create and execute a new work

What puzzles me is that after the worker thread completes, I can still see it running in the debugging window of NetBeans (for example, as swingworker-pool-3-thread-1) or in the Windows Task Manager, the number of threads running after the thread completes does not decrease) The number of swingworker threads is limited to 10, but it's embarrassing for me to let them run

In the case of simple thread usage:

Thread th = new Thread(new Runnable() {

    public void run() {
        int a = 0;
    }
});
th.start();

The thread disappears automatically after execution

Is this swingworker behaving normally?

Solution

Yes, it's normal As the name of the thread indicates, the swing worker's model (background) action is delegated as thread pool When the work is finished, the thread returns to the pool so that other workers can use it This eliminates some overhead of creating / destroying potentially expensive threads

edit

By the way, background threads won't last forever Looking at the source code of swingworker, I see:

//from SwingWorker.java (c) Sun Microsystems/Oracle 2009
executorService =
            new ThreadPoolExecutor(1,MAX_WORKER_THREADS,10L,TimeUnit.MINUTES,new LinkedBlockingQueue<Runnable>(),threadFactory);

This indicates that the thread will crash after being idle for 10 minutes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>