title: Java Thread does not terminate
body: <p>I am trying to animate a triangle when two triangles have been clicked twice. As soon as I click them twice triangle 0 starts moving until moveX reaches 20, while(moveX &lt; 20),  but then it doesn't seem to reach a terminated state, it doesn't go inside : if(t.getState() == Thread.State.TERMINATED), any ideas?</p>
  
  <pre><code>public class secondFrame extends JFrame implements Runnable {
  
  }
  
  
  public secondFrame(ChallengesDialog dialog) {
  
      component = new secFrameDrawComponent();
      createComponent();
  
      Thread t = new Thread(this);
      t.start();
  }
  
  private void createComponent() {
  
      sGen = new shapeGenerator(434, 231);
  
      createT();
  }
  
  class AddComponentListener implements MouseListener {
  
      public void mouseClicked(MouseEvent event) {
  
  
      for(int i = 0; i &lt; count; i++) {
          if (component.getTri(i).contains(event.getX(), event.getY())) {
              component.setShapeFill("triangle", i);
              component.repaint();    
  
              countColors(i);
  
              setChallenge();
          }
  ...
  MouseListener listener = new AddComponentListener();
  component.addMouseListener(listener);                       
  
  public void setChallenge() {
  
      //two red triangles
      if(challenge == 1 &amp;&amp; triColorCount[1] == 2) {
          Thread t = new Thread(component);
          t.start();
  
          System.out.println("Thread's state: " + t.getState());
  
          if(t.getState() == Thread.State.TERMINATED) {
              //Unreachable
              System.out.println("Thread completed");
          }                   
      }
  }
  ...
  
  public void run() {
  
      while(threadFlag) { 
          ..
          timeLabel.setText("Elapsed time: " + elapsedTime);
          ..                      
      }
   }
  
  public class secFrameDrawComponent extends JComponent implements Runnable {
  
      public void modTriangle(int x, int y) {
  
          sGen.setTriangleOffset(x, y);
  
          triangle[0] = sGen.getTriangle();
      }
  
      public void paintComponent(Graphics g) {
  
          Graphics2D g2 = (Graphics2D)g;
  
          draw(g2);       
      }
  
      public void run() {
  
          while(moveX &lt; 20) {
  
              moveX+= 1;
              modTriangle(moveX, 0);
              repaint();
  
              try {
                  Thread.sleep(170);
              } catch (InterruptedException e) {  
                  e.printStackTrace();
              }
              System.out.println("moveX = " + moveX);
          }
          System.out.println("Thread completed exec");        
      }
  
      public void draw(Graphics2D g2) {
  
          g2.setStroke(new BasicStroke(2));
  
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
              RenderingHints.VALUE_ANTIALIAS_ON);
  
  
          modTriangle(moveX, 0);
          g2.draw(triangle[0]);
          g2.draw(triangle[1]);
      }
  </code></pre>