Java thread doesn't stop/interrupt

Viewed 10424

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.

The run method for this thread look like this:

public void run() {
    while (true)
    {
        String M = recieveMessage();
        char[] chars = M.toCharArray();
        if(chars[0]==robotName||chars[0]=='0')
            messages.add(M);                                                
     }
}

In the main controller I have code that look like this:

MessageThread MT = new MessageThread(messages, receiver,getName());
MT.start();
for (int i = 0; i < 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());

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.

If I call interrupt() or MT = null but interrupted() it returns false and MT continues to run. Is there anything wrong in my code?

I read some topics like:

http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

How do you kill a Thread in Java?

interrupt() doesn't work

Java: How interrupt/stop a thread?

and so on but I couldn't find any useful answer.

Edit

Thanks everyone, I've made changes to my code. I added this to the MessageThread class:

private volatile boolean isRunning = true;

Then I used while(isRunning) instead of while(true) and I added

public void kill()
{
    isRunning = false;
}

and called MT.kill() instead of MT.interrupt().

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 "a thread must support its own interruption". So do I have to overwrite the interrupt() method? I can't call interrupt to terminate a thread?

Thanks again.

1 Answers
Related