title: I want to pause and resume my program
body: <p>I have a program that estimates the value of PI every million trials. However, I want the program to be paused when I click on pause and to resumes when I click on run, using <code>wait()</code> and <code>notify()</code>. </p>
  
  <p>I must use multiple threads as well as a <code>Boolean</code> as a signal of where should it pause and run, but I do not know how. I am confused.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>package com.company;
  
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  public class Ex03 extends JFrame implements Runnable, ActionListener {
      int n = 0;
      int c = 0;
      double Pi;
      int change = 1000000;
      boolean runing = true;
  
      JLabel actualpi = new JLabel("The Actual value of PI   " + Math.PI);
      JLabel estimation = new JLabel("Current Estimate:  ");
      JLabel tri = new JLabel("Number Of Trials:  " + n);
      JButton run = new JButton("Run");
      JButton pause = new JButton("Pause");
  
      public Ex03() {
          super("Ex 03");
          setLayout(new GridLayout(4, 1));
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setSize(400, 400);
          setVisible(true);
          add(actualpi);
          add(estimation);
          add(tri);
          add(run);
  
          run.addActionListener(this);
          pause.addActionListener(this);
      }
  
      public void actionPerformed(ActionEvent e) {
          Thread thread = new Thread(this);
          if (e.getSource() == run) {
              thread.start();
              remove(run);
              add(pause);
          } else if (e.getSource() == pause) {
              remove(pause);
              add(run);
              try {
                  thread.wait();
              } catch (InterruptedException e1) {
              }
          }
      }
  
      public void run() {
          n++;
          while (runing) {
              double x = Math.random();
              double y = Math.random();
              if (((x * x) + (y * y)) &lt;= 1)
                  c++;
              n++;
              Pi = (4.0 * (double) c / n);
  
              if (n == change) {
                  estimation.setText("Current Estimate:  " + Pi);
                  tri.setText("Number Of Trials:  " + n);
                  change = change + 1000000;
              }
  
              try {
                  Thread.sleep(0);
              } catch (InterruptedException e) {
              }
          }
      }
  
      public static void main(String[] args) {
          new Ex03();
      }
  }
  </code></pre>