title: Interrupt Threads with a JButton
body: <p>I'm currently having lessons about Java threads in my college,and today's exercise was about creating two threads.
   <strong>Thread A</strong> prints random numbers from 1 to 9 with no sleep, and <strong>Thread B</strong> from 1000 to 9999 with 50 sleep, it's an infinite loop til I decide to press the <strong>stop button</strong> which is a JButton that <strong>interrupts both threads</strong>.
  Thing is, I'm having some trouble trying to stop the threads with one button, and mainly trying to find out how to work it out ,  and how to create an actionEvent for that purpose.</p>
  
  <p>This is the code I have so far:</p>
  
  <pre><code>    import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JButton;
      import javax.swing.JFrame;
  
  public class RandomNumbers extends Thread {
      long time;
      long min;
      long max;
      private JFrame window;
      private JButton stopButton;
  
      public RandomNumbers(long min, long max, long time) {
          this.min = min;
          this.max = max;
          this.time = time;
          new Window();
      }
  
      public void run() {
          try {
              while (true) {
                  System.out.println((int) ((Math.random() * max) + min));
                  Thread.sleep(time);
              }
          } catch (Exception e) {
              System.out.println("I was interrupted!");
          }
      }
  
      public class Window {
          public Window() {
              window = new JFrame("Stop Button");
              stopButton = new JButton("Stop");
              stopButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent arg0) {
                      // ThreadA.interrupt(); //problem in here , what to do ? //****
                      // ThreadB.interrupt();
  
                  }
              });
  
              window.getContentPane().add(stopButton);
              window.setSize(100, 100);
              window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              window.setVisible(true);
          }
      }
  
      public static void main(String[] args) throws InterruptedException {
  
          Thread threadB = new RandomNumbers(1, 9, 50);
          Thread threadA = new RandomNumbers(1000, 8999, 0);
          threadB.start();
          threadA.start();
      }
  }
  </code></pre>
  
  <p>Also there's another problem on this code where it'll create <strong>2 stop buttons</strong> , 1 for each thread, as it's no the constructor.. I'm kinda lost so I need some guidance here.
  Any help is appreciated, thanks a lot !</p>