如何使用 JButtons 启动和暂停 SwingWorker 线程 [英] How to use JButtons to Start and Pause SwingWorker thread
问题描述
我正在尝试在 Swing 中学习线程.
I'm trying to learn Threads in Swing.
我有一个带有 JProgressBar(进度)、五个 JButton(开始、暂停、恢复、取消、关闭)和一个 JLabel(标签 1)的框架.
I have a Frame with a JProgressBar (progress), five JButtons (Start, Suspend, Resume, Cancel, Close), and a JLabel (label1).
框架打开.仅启用启动.开始调用我的班级进度:
The frame opens. Only Start is enabled. Start calls my class Progressor:
一劳永逸地再次更新
进度条;//声明为类变量,在构造函数中初始化 new
并在重写的 done
方法中再次初始化
Progressor progressor; //declared as class variable, initialized new
in constructor and again in overridden done
method
这是 ButtonListener 类:
Here's the ButtonListener class:
public class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbStart) {
progressor.execute();
label1.setText("Progressing ...");
jbCancel.setEnabled(true);
jbResume.setEnabled(true);
jbSuspend.setEnabled(true);
jbClose.setEnabled(true);
}
if(e.getSource() == jbCancel) {
progressor.cancel(true);
label1.setText("Progress Canceled");
}
if (e.getSource() == jbSuspend) {
label1.setText(progressor.suspendProgress());
}
if (e.getSource() == jbResume) {
label1.setText(progressor.resumeProgress());
}
if (e.getSource() == jbClose) {
dispose();
}
}
}//按钮监听器
这是 SwingWorker 类:
Here's the SwingWorker class:
public class Progressor extends SwingWorker<Void, Integer> {
private volatile boolean suspend = false;
private Object lock = new Object();
@Override
protected Void doInBackground() {
for (int i = 0; i <= 10; i++) {
checkForSuspend();
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
publish(i);
}
return null;
}
@Override
protected void process(List<Integer> list) {
int value = list.get(list.size() - 1);
progress.setValue(value);
}
public void checkForSuspend() {
synchronized (lock) {
while (suspend) {
try {
lock.wait();
} catch (InterruptedException ie){
}
}
}
}//checkForSuspend
@Override
protected void done() {
label1.setText("All Done. Press close to exit");
progressor = new Progressor();
}
public synchronized String suspendProgress() {
suspend = true;
return "Progress suspended ...";
}
public synchronized String resumeProgress() {
synchronized (lock) {
suspend = false;
lock.notify();
return "Progress resumed ...";
}
}
}//Progressor class
除了 cancel
没有实际取消线程外,一切正常(进度条继续).
Everything works except the cancel
doesn't doesn't actually cancel the thread (the progress bar continues).
在取消之前我应该暂停吗?
Should I suspend it before canceling?
推荐答案
这个 How to Pause and Resume a Thread in Java from another Thread 问题看起来与您的非常相似,答案中有一些很好的例子.
This How to Pause and Resume a Thread in Java from another Thread question looks very similar to yours and has some nice examples in the answers.
至于你自己的代码以及为什么它不起作用:
As for your own code and why it does not work:
- 您每次点击都会创建一个新的进度条.您应该使用和控制一个,而不是每次都创建新的.
暂停时,进度条完成工作而不是暂停.正如上述问题所述 - 您应该在计算的某些点查看标志并对其采取行动.示例:
- You create a new progressor on every click. You should be using and controlling one, instead of creating new ones every time.
When suspending your progressor finishes work instead of suspending. As the above question states - you should be looking at the flag at some points of your computation and acting on it. Example:
while (!cancel) {
if (!suspended) {
for (int i = 1; i <= 10; i++) {
Thread.sleep(1000);
publish(i);
}
}
}
上面的代码会在下一次达到 10 时暂停(除非您之前恢复它),并在您按下取消时完成(取消需要以明显的方式作为额外标志添加).
The above code will suspend when it next reaches 10 (unless you resumed it before that), and finish when you press cancel (Cancel needs to be added as an extra flag in the obvious manner).
这篇关于如何使用 JButtons 启动和暂停 SwingWorker 线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!