gpt4 book ai didi

java - 如何暂停所有正在运行的线程?然后恢复?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:44 25 4
gpt4 key购买 nike

我已经看过这个问题:How pause and then resume a thread?

我在 stackoverflow 中看到了很多与我的问题相关的问题,但我无法理解它们,因为它们很抽象,对我的情况不够具体。

有 2 个倒计时标签。单击 Start Button 时,将执行倒计时。同样,当你点击Pause Button时,它应该被暂停。但是,我收到一个错误:Exception in thread "AWT-EventQueue-0"java.lang.IllegalMonitorStateException

2 线程已启动,但我无法使用 wait() 方法停止它。请让我知道如何停止线程,并实现 resume 按钮。谢谢。下面的简单示例

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class FirstCountDown extends SwingWorker<Integer, Integer> {

public Integer doInBackground() {
for(int i=0; i<1000; i++){
CountDown.count1.setText(String.valueOf(1000-i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}

class SecondCountDown extends SwingWorker<Integer, Integer> {

public Integer doInBackground(){
for(int i=0; i<1000; i++){
CountDown.count2.setText(String.valueOf(1000-i));
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}

class CountDown extends JFrame {

static JLabel count1;
static JLabel count2;
static JButton startButton;
static JButton pauseButton;
static JButton resumeButton;
FirstCountDown first = new FirstCountDown();
SecondCountDown second = new SecondCountDown();

public CountDown(){
count1 = new JLabel("1000");
count2 = new JLabel("1000");
startButton = new JButton("start");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
first.execute();
second.execute();
}
});
pauseButton = new JButton("pause");
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try {
first.wait();
second.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

resumeButton = new JButton("resume");

setSize(300,100);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(count1);
add(count2);
add(startButton);
add(pauseButton);
add(resumeButton);
setVisible(true);

}

public static void main(String args[]) {

CountDown g = new CountDown();

}
}

最佳答案

您不能通过调用 wait() 来暂停线程执行。 wait() 挂起当前 线程,直到另一个线程调用同一对象上的notify()notifyAll()上一个线程调用了 wait()。此外,为了在对象上调用 wait()notify()notifyAll(),调用线程必须持有 对象的监视器通过做

synchronized(object) {
object.wait();
}

为了暂停计数,您需要一个标志并提供两种方法来暂停和恢复计数。像这样的东西:

class FirstCountDown extends SwingWorker<Integer, Integer> {

private _suspended = false;

public synchronized void suspend() { _suspended = true; notify(); }
public synchronized void resume() { _suspended = false; notify(); }

public Integer doInBackground() {
for(int i=0; i<1000; i++) {
synchronized(this) {
while (_suspended) {
wait(); // The current thread will block until some else calls notify()
// Then if _suspended is false, it keeps looping the for
}
}
CountDown.count1.setText(String.valueOf(1000-i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}

关于java - 如何暂停所有正在运行的线程?然后恢复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20666454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com