在这种情况下,当我想停止 SwingWorker 时,我做错了什么?
What did I wrong when I want to stop SwingWorker in this case?
public class Worker extends SwingWorker<Integer, String> {
private JLabel screen;
public Worker(JLabel screen) {
this.screen = screen;
}
@Override
protected Integer doInBackground() throws Exception {
for (; ; ) {
publish(String.valueOf(Calendar.getInstance().getTimeInMillis()));
}
}
@Override
protected void process(List<String> chunks) {
screen.setText(chunks.get(0));
}
}
在表格中:
public class Form extends JPanel{
private JButton startButton;
private JPanel rootPanel;
private JButton stopButton;
private JLabel screen;
private Worker worker;
public Form() {
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
worker = new Worker(screen);
worker.execute();
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
worker.cancel(true);
System.out.println(worker.isDone());
System.out.println(worker.isCancelled());
}
});
}
private void createUIComponents() {
rootPanel = this;
}
}
我尝试用 Tread 编写相同的代码,但它也不起作用。单击停止按钮后控制台输出:
true
true
所以,worker 已经完成了,但是程序仍然继续显示毫秒。什么是post-死亡生命?如果使用 Thread 同样的事情:方法 isAlive() return "false".
根据您的代码,您似乎需要一个带有循环的工作程序来保持 运行,直到主线程告诉它停止 运行,这是通过 worker.cancel(true);
完成的.问题是你正在取消它,但没有做任何事情来通知循环本身停止迭代。要解决此问题,您应该更改
for (; ; ) {
publish(String.valueOf(Calendar.getInstance().getTimeInMillis()));
}
到
while(!isCancelled()){
publish(String.valueOf(Calendar.getInstance().getTimeInMillis()));
}
请注意 java 文档中 'isCancelled' 的措辞:
isCancelled()
Returns true if this task was canceled before it completed normally.
Since the loop never closes on its' own, it will never complete normally.
public class Worker extends SwingWorker<Integer, String> {
private JLabel screen;
public Worker(JLabel screen) {
this.screen = screen;
}
@Override
protected Integer doInBackground() throws Exception {
for (; ; ) {
publish(String.valueOf(Calendar.getInstance().getTimeInMillis()));
}
}
@Override
protected void process(List<String> chunks) {
screen.setText(chunks.get(0));
}
}
在表格中:
public class Form extends JPanel{
private JButton startButton;
private JPanel rootPanel;
private JButton stopButton;
private JLabel screen;
private Worker worker;
public Form() {
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
worker = new Worker(screen);
worker.execute();
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
worker.cancel(true);
System.out.println(worker.isDone());
System.out.println(worker.isCancelled());
}
});
}
private void createUIComponents() {
rootPanel = this;
}
}
我尝试用 Tread 编写相同的代码,但它也不起作用。单击停止按钮后控制台输出:
true
true
所以,worker 已经完成了,但是程序仍然继续显示毫秒。什么是post-死亡生命?如果使用 Thread 同样的事情:方法 isAlive() return "false".
根据您的代码,您似乎需要一个带有循环的工作程序来保持 运行,直到主线程告诉它停止 运行,这是通过 worker.cancel(true);
完成的.问题是你正在取消它,但没有做任何事情来通知循环本身停止迭代。要解决此问题,您应该更改
for (; ; ) {
publish(String.valueOf(Calendar.getInstance().getTimeInMillis()));
}
到
while(!isCancelled()){
publish(String.valueOf(Calendar.getInstance().getTimeInMillis()));
}
请注意 java 文档中 'isCancelled' 的措辞:
isCancelled()
Returns true if this task was canceled before it completed normally. Since the loop never closes on its' own, it will never complete normally.