好的,所以我一直在玩SwingWorker,并得到了一些简化的代码来更新gui,但我无法弄清楚如何让线程正常终止当它完成时。目前,它只是通过停止选项终止。我将如何设置它,以便在完成其过程时正确终止线程?目前,在return null;
之后,它进入包装行并挂起。Java SwingWorker没有终止任务完成
我的代码如下:
package concurrency;
import java.util.List;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class PBTest extends JFrame implements ActionListener {
private final GridBagConstraints constraints;
private final JProgressBar pb, pbF;
private final JButton theButton;
private PBTask pbTask;
private JProgressBar makePB() {
JProgressBar p = new JProgressBar(0,100);
p.setValue(0);
p.setStringPainted(true);
getContentPane().add(p, constraints);
return p;
}
public PBTest() {
super("PBTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Make text boxes
getContentPane().setLayout(new GridBagLayout());
constraints = new GridBagConstraints();
constraints.insets = new Insets(3, 10, 3, 10);
pb = makePB();
pbF = makePB();
//Make buttons
theButton = new JButton("Start");
theButton.setActionCommand("Start");
theButton.addActionListener(this);
getContentPane().add(theButton, constraints);
//Display the window.
pack();
setVisible(true);
}
private static class UpdatePB {
private final int pb1, pb2;
UpdatePB(int pb1s, int pb2s) {
this.pb1 = pb1s;
this.pb2 = pb2s;
}
}
private class PBTask extends SwingWorker<Void, UpdatePB> {
@Override
protected Void doInBackground() {
int prog1 = 0;
int prog2 = 0;
Random random = new Random();
while (prog2 < 100) {
if(prog1 >= 100) {
prog1 = 0;
}
//Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {}
//Make random progress.
prog1 += random.nextInt(10);
prog2 += random.nextInt(5);
publish(new UpdatePB(prog1, prog2));
}
return null;
}
@Override
protected void process(List<UpdatePB> pairs) {
UpdatePB pair = pairs.get(pairs.size() - 1);
pb.setValue(pair.pb1);
pbF.setValue(pair.pb2);
}
}
public void actionPerformed(ActionEvent e) {
if ("Start" == e.getActionCommand() && pbTask == null) {
theButton.setText("Stop");
theButton.setActionCommand("Stop");
(pbTask = new PBTask()).execute();
} else if ("Stop" == e.getActionCommand()) {
theButton.setText("Start");
theButton.setActionCommand("Start");
pbTask.cancel(true);
pbTask = null;
} else {
alertMsg("Thread still running.");
}
}
static void alertMsg(String theAlert) {
JOptionPane.showMessageDialog(null, theAlert);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PBTest();
}
});
}
}
注:这是基本的Java教程‘脚蹼’的例子的改变......我没有那么多程序员的代码黑客(/伤心面对/,大声笑),此刻,所以我有点不知道下一步该去哪里。
无论如何,该代码的工作原理,直到它完成。我尝试添加done()
方法,但它永远不会尝试运行它,它总是只进入包行(当通过调试器时)并挂起。我应该返回一个非零值的东西吗?
在此先感谢您的帮助!
使用''==操作符不要拿'String's。使用['String.equals()'](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object))方法。 – Crozin 2013-03-01 20:06:48
感谢您的提示。这就是它在Java教程网站的脚本示例中的设置。我用==比较字符串的方法遇到了麻烦,所以希望这有助于这一点。谢谢!现在,如果我能弄清楚如何让线程正常完成而不用强制解决。 – DGolberg 2013-03-01 20:11:56