I implemented my own Thread in Java this way :
public class MyThread extends Thread{
protected boolean exit = false;
protected void kill(){
exit = true;
}
}
So when I create a thread, the method run() looks like :
public void run() {
...
while(!exit) {
...
}
}
My question is the following : I'm using my thread class to receive and send datagrams. But when the thread is waiting for a datagram, if I call the function MyThread.kill(), the thread will still wait a next packet before ending itself... How can I kill my thread without waiting for an another packet ? Thank you very much
After setting the exit flag, send the thread a datagram yourself to unblock it. By the way,
exit
should bevolatile
.