title: Java - Thread Doesn't Stop
body: <p>I have the following class that's nested in another class (that also extends thread)</p>
  
  <pre><code>public class Miner extends Thread {
      private volatile boolean running = true;
  
      public void setRunning(boolean running) {
          this.running = running;
      }
      public boolean getRunning() {
          return running;
      }
  
      private void MainLoop() {
          if(!running) {
              robot.keyRelease(KeyEvent.VK_D);
              return;
          }
          robot.keyPress(KeyEvent.VK_D);
          robot.keyRelease(KeyEvent.VK_D);
          try {
              Thread.sleep(100 + (int)(Math.random()*randInt));
          } catch(Exception e) {}
  
          MainLoop();
      }
  
      @Override
      public void run() {
          MainLoop();
      }
  }
  </code></pre>
  
  <p>Then in the class that Miner is nested in I have</p>
  
  <pre><code>private void MainLoop() throws AWTException {
      Miner miner = new Miner();
      miner.start();
      ... does other stuff ...
      while(miner.getRunning())
          miner.setRunning(false);
      ... do more stuff ...
      MainLoop();
  }
  </code></pre>
  
  <p>However, after my attempt at setting running to false and stopping the miner thread and doing "do more stuff", that whole time I am doing "do more stuff" it is still pressing the D key.<br><br>
  Full MainLoop() method for class containing Miner.</p>
  
  <pre><code>private void MainLoop() throws AWTException {
          if(stop)
              return;
          Miner miner = new Miner();
          miner.start();
          while(true) {
              BufferedImage screenCap = robot.createScreenCapture(new Rectangle(x, y, 1, 1));
              int c = screenCap.getRGB(0,0);
              int  red = (c &amp; 0x00ff0000) &gt;&gt; 16;
              int  green = (c &amp; 0x0000ff00) &gt;&gt; 8;
              int  blue = c &amp; 0x000000ff;
              Color color = new Color(red,green,blue);
              if(color.equals(DRILL_COLORS[0]) || color.equals(DRILL_COLORS[1]))
                  break;
              try {
                  Thread.sleep(500);
              } catch(Exception e) {}
              if(stop)
                  return;
          }
          while(miner.getRunning())
              miner.setRunning(false);
          count++;
          if(count &gt;= 3) {
              up = !up;
              count = 0;
          }
  
          switch(moveDir) {
              case 0:
                  if(up)
                      holdKey(KeyEvent.VK_UP, 100 + (int)(Math.random()*randInt));
                  else
                      holdKey(KeyEvent.VK_DOWN, 100 + (int)(Math.random()*randInt));
                  break;
              case 1:
                  if(up)
                      holdKey(KeyEvent.VK_RIGHT, 100 + (int)(Math.random()*randInt));
                  else
                      holdKey(KeyEvent.VK_LEFT, 100 + (int)(Math.random()*randInt));
                  break;
          }
  
          switch(wallLoc) {
              case 0:
                  holdKey(KeyEvent.VK_UP, 50);
                  break;
              case 1:
                  holdKey(KeyEvent.VK_DOWN, 50);
                  break;
              case 2:
                  holdKey(KeyEvent.VK_RIGHT, 50);
                  break;
              case 3:
                  holdKey(KeyEvent.VK_LEFT, 50);
                  break;
          }
  
          MainLoop();
      }
  </code></pre>
  
  <p>The goal is basically to have the Miner thread press down the D key until it detects a certain color on a point on my screen. Then it should stop the miner thread while it moves my character a bit and then re-call the method and start all over again. But the miner thread runs the whole time.</p>