SwingWorker thread does not close, even the task is completed?

I have a swingworker in my java project. I am using netbean profiler to monitor the flow. I do not know why the swingworker thread still exists on the monitor of the NetBeans profiler and is in the "Wait" state. In other words, if I press the b button 10 times, there are 10 swingworker threads! Thanks.

public static void main(String[] args) { // TODO code application logic here final JFrame f = new JFrame(); f.setLayout(new BorderLayout()); f.setSize(400, 400); b = new JButton("B1"); f.add(b,BorderLayout.CENTER); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new SwingWorker() { @Override protected Object doInBackground() throws Exception { return null; } }.execute(); } }); f.setVisible(true); } 
+4
source share
2 answers

To clarify my comment, check the output of this modification of your code:

 import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; public class Foo002 { public static void main(String[] args) { final JFrame f = new JFrame(); f.setLayout(new BorderLayout()); f.setSize(400, 400); JButton b = new JButton("B1"); f.add(b, BorderLayout.CENTER); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new SwingWorker() { @Override protected Object doInBackground() throws Exception { Thread current = Thread.currentThread(); System.out.printf("ID: %d, Name: %s%n", current.getId(), current.getName()); System.out.println("Active Count: " + Thread.activeCount()); return null; } }.execute(); } }); f.setVisible(true); } } 
+7
source

no, this is not a SwingWorker or something similar, please read the tutorial on how to build and use SwingWorker , check what happened if the () method was not done or the link to Future<?> (copied from the tutorial) is SwingWorker worker = new SwingWorker<ImageIcon[], Void>()

+2
source

All Articles