stopping threads

I am building an agent-based application where all agent classes extend from the Thread class and therefore can run on their own in my program.

The agents are started from my gui as when I press the run button:

Agent agent1 =new Agent();

agent1.start();

...

Agent agentn =new Agent();

agentn.start

Now after some time, I would like to stop the agents, so i have implemented a button quit. When i press this button quit, the interrupt method is called, but nothing happens.

How can i stop (and destroy) these Agents?

[640 byte] By [bvdheijdena] at [2007-11-26 22:28:32]
# 1

you can not kill/stop threads in java 1.4 and up and for a good reason.

what you should do is have your threads contain a field like keepAlive with default value true and a method like stopAgentThread that will set it to false.

in your run method you should check this field every now and then and if its false, exit the run method.

Once the run method ends the thread ends as well.

sushika at 2007-7-10 11:31:46 > top of Java-index,Desktop,Developing for the Desktop...
# 2

Ok, I had already such a construction, but needed the hint in the right direction.

Next up, the server connection...

The receiveChannel.accept() also runs in a seprarate thread, als the main loop is checked on a variable which is set to false when the system should stop.

I have implemented:

protected void finalize()

{

try

{

receiveChannel.close();

} catch (IOException e1)

{

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

and i have tried it using this in the while(var=true) loop (when var becommes false)

try

{

Thread.sleep(5000);

receiveChannel.close();

} catch (IOException e1)

{

// TODO Auto-generated catch block

e1.printStackTrace();

}

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

But nothing seems to close my threads... Any help?

bvdheijdena at 2007-7-10 11:31:46 > top of Java-index,Desktop,Developing for the Desktop...
# 3
http://java.sun.com/docs/books/tutorial/essential/concurrency/interrupt.htmlHope that helps.Regards-.
Rabinoa at 2007-7-10 11:31:46 > top of Java-index,Desktop,Developing for the Desktop...