title: Java thread doesn't stop/interrupt
body: <p>I'm trying to terminate a thread but it doesn't interrupt or stop. All of this are part of controller of a software called Webots. I use this to simulate a multi robot system. In the controller of each robot, I start a thread which receive messages through robots receivers. This thread must start at first, and terminate when simulation ends.</p>
  
  <p>The run method for this thread look like this:</p>
  
  <pre><code>public void run() {
      while (true)
      {
          String M = recieveMessage();
          char[] chars = M.toCharArray();
          if(chars[0]==robotName||chars[0]=='0')
              messages.add(M);                                                
       }
  }
  </code></pre>
  
  <p>In the main controller I have code that look like this:</p>
  
  <pre><code>MessageThread MT = new MessageThread(messages, receiver,getName());
  MT.start();
  for (int i = 0; i &lt; 100; i++)
  {
      try
      {
           Thread.sleep(25);  } catch (InterruptedException e) {      e.printStackTrace();    }
      System.out.println(messages.get(messages.size()-1));                
   }
   MT.interrupt();//MT = null;
   System.out.println(MT.interrupted());
  </code></pre>
  
  <p>It's not important what I do in my main controller, so don't judge it. For example, messages is an ArrayList. It's like a buffer which MT put messages in and the main thread reads from. I use it because the receiver and emitter are not synchronized. </p>
  
  <p>If I call interrupt() or MT = null but interrupted() it returns false and MT continues to run. Is there anything wrong in my code?</p>
  
  <p>I read some topics like:</p>
  
  <p><a href="http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html</a></p>
  
  <p><a href="https://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java">How do you kill a Thread in Java?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8627719/interrupt-doesnt-work">interrupt() doesn&#39;t work</a></p>
  
  <p><a href="https://stackoverflow.com/questions/6599202/java-how-interrupt-stop-a-thread">Java: How interrupt/stop a thread?</a></p>
  
  <p>and so on but I couldn't find any useful answer.</p>
  
  <h2>Edit</h2>
  
  <p>Thanks everyone, I've made changes to my code. I added this to the MessageThread class: </p>
  
  <pre><code>private volatile boolean isRunning = true;
  </code></pre>
  
  <p>Then I used <code>while(isRunning)</code> instead of <code>while(true)</code> and I added</p>
  
  <pre><code>public void kill()
  {
      isRunning = false;
  }
  </code></pre>
  
  <p>and called <code>MT.kill()</code> instead of <code>MT.interrupt()</code>.</p>
  
  <p>It worked but I couldn't find out what's wrong with interrupt(). I read the link which @ExtremeCoders recommended. However, I'm still confused. It says <em>"a thread must support its own interruption".</em> So do I have to overwrite the interrupt() method? I can't call interrupt to terminate a thread?</p>
  
  <p>Thanks again.</p>