title: How to access the running thread/runnable?
body: <p>I have a thread running but from outside I can't bypass a value to stop that thread. How can I send false/true value inside <code>Mytest()</code> or call running thread public methods? When I press the button1? 
  ex: <code>thread.interrupt();</code> <code>runnable.stop();</code> or <code>runnable.start();</code></p>
  
  <pre><code>// Main
  public class Main extends JFrame
  {
    public static Runnable runnable;
    public static Thread thread;
    private JButton b1    = new JButton("Start/Stop");
  
    public void init() 
    {    
      //Execute a job on the event-dispatching thread:
      try {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable() 
         {
           public void run() 
           {
              createGUI();
           }
          });
       } catch (Exception e) { 
         System.err.println("createGUI didn't successfully complete");
       }
    }
  
    public void createGUI()
    {
      Container cp = getContentPane();
      b1.addActionListener(new button1()); cp.add(b1);
      runnable = new Mytest();
      thread = new Thread(runnable);
      thread.start();
    }
  }
  
  // Button 1 - [problem to go  inside a running thread]
  public class button1 implements ActionListener 
  {
    public void actionPerformed(ActionEvent e) 
    {
      System.out.println("button pressed - need to access ");
        //thread.interrupt();       runnable.stop(); //or runnable.start();
    }
  }
  
  // Running - Thread
  public class Mytest implements Runnable
  {
    public static boolean onoff = false;
    public static boolean status = false;
  
    public void run()
    {
      while(true) 
      {
        if (onoff) 
        {
           return;
         } else { 
           if (status==false) System.out.println("running"); 
         }
      }
    }
    public static void stop() { status = true; onoff=true; }
    public static void start() { status = false; onoff = false; }
  }
  </code></pre>
  
  <p><strong>Follow up (proof read):</strong></p>
  
  <pre><code>Step 1:
  
  /* Main -  boot/startup */
  public class Main extends JFrame
  {
      public static Mytest runnable;  // wrong: public static Runnable runnable;
      public static Thread thread;
      private JButton b1    = new JButton("Start");
      private JButton b2    = new JButton("Stop");  
  
    public void init() 
    {    
      // Execute a job on the event-dispatching thread:
      // In case Freezed for heavy lifting
      try {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable() 
         {
           public void run() 
           {
              createGUI();
           }
         });
       } catch (Exception e) { 
         System.err.println("createGUI didn't successfully complete");
       }
    }
  
    public void createGUI()
    {
      Container cp = getContentPane();
      b1.addActionListener(new button1()); 
      cp.add(b1);
  
      runnable = new Mytest();
      thread = new Thread(runnable);    
          try {
                  thread.sleep(100);  // value is milliseconds        
                  thread.start();     
          } catch (InterruptedException e) {              
          }
    }
  
    public static void main(String[] args)
    {        
      run(new Main(), 500, 500);
    }
  
    public static void run(JFrame frame, int width, int height) 
    {        ...
      frame.setVisible(true);
    }
  }
  
  /* To start */
  public class button1 implements ActionListener 
  {
    public void actionPerformed(ActionEvent e) 
    {
      runnable.start();
    }    
  }
  
  /* To stop */
  public class button2 implements ActionListener 
  {
    public void actionPerformed(ActionEvent e) 
    {
      runnable.stop();
    }    
  }
  
  Step 2:
  
  /* Thread deals */
  public class Mytest implements Runnable
  {
    private static volatile boolean running = true;
  
    public void run()
    {
      while(running) 
      {
        // do stuff
      }
    }
    public void start() { running = true; }
    public void stop()  { running = false;}
  }
  </code></pre>