title: How to pause all running threads? and then resume?
body: <p><strong>I have already seen this question</strong> : <a href="https://stackoverflow.com/questions/16758346/how-pause-and-then-resume-a-thread">How pause and then resume a thread?</a></p>
  
  <p>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.</p>
  
  <p>There are 2 Count Down Labels. When you click <code>Start Button</code>, the countdown is executed. In the same way, when you click <code>Pause Button</code>, it should be paused. However, I am getting an error: <code>Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException</code> </p>
  
  <p>2 Threads are started, but I can't stop it with the <code>wait()</code> method. Please let me know how can I stop threads, and implement <code>resume</code> button. Thanks. Easy Example below</p>
  
  <pre><code>import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.*;
  
  class FirstCountDown extends SwingWorker&lt;Integer, Integer&gt; {
  
  public Integer doInBackground() {
      for(int i=0; i&lt;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&lt;Integer, Integer&gt; {
  
  public Integer doInBackground(){
      for(int i=0; i&lt;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();
  
  }
  }
  </code></pre>