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

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

我在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 { 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 { 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() ,调用线程必须通过执行操作来保存该对象的监视器

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

为了暂停你的计数,你需要一个标志,并提供两种方法来暂停和恢复你的计数。 就像是:

 class FirstCountDown extends SwingWorker { 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; } }