如何暂停所有正在运行的线程?然后恢复? [英] How to pause all running threads? and then resume?

查看:260
本文介绍了如何暂停所有正在运行的线程?然后恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个问题:如何暂停然后恢复线程?

我在stackoverflow中看到了很多与我的问题有关的问题,但是我听不懂,因为它们是抽象的,对我的情况还不够具体.

I have seen many questions in stackoverflow related to my issue, but I couldn't understand them because they are abstract and not specific enough to my sitiuation.

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

There are 2 Count Down Labels. When you click Start Button, the countdown is executed. In the same way, when you click Pause Button, it should be paused. However, I am getting an error: Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException

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

2 Threads are started, but I can't stop it with the wait() method. Please let me know how can I stop threads, and implement resume button. Thanks. Easy Example below

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()挂起当前线程,直到另一个线程在与先前调用wait()的线程相同的对象上调用notify()notifyAll().此外,为了在对象上调用wait()notify()notifyAll(),调用线程必须通过执行

You can't suspend a thread execution by invoking wait() on it. wait() suspend the current thread until another thread calls notify() or notifyAll() on the same object the previous thread called wait() on. Moreover, in order to call wait(), notify() or notifyAll() on an object, the calling thread must hold that object's monitor by doing

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

要暂停计数,您需要一个标志并提供两种方法来暂停和恢复计数.像这样:

In order to suspend your counting, you need a flag and provide two methods to suspend and resume your counting. Something like:

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;
    }
}

这篇关于如何暂停所有正在运行的线程?然后恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆