hello everyone
So how do i kill a thread in java if it has been started and wont be needed any furthur.
just curious,
any help will be greatly appreciated
thank you
Denis
Printable View
hello everyone
So how do i kill a thread in java if it has been started and wont be needed any furthur.
just curious,
any help will be greatly appreciated
thank you
Denis
Actually you can use Thread.stop(), but it's deprecated because it may can cause for thread deadlocks.
So in Java, it's not possible to kill a thread directly. You may use some meaningful implementation for that. If you need more help on this, let me know here.
Code:public class Bogus {
public static void main (String[] args) {
Thread t = new Thread(
new Runnable() {
public void run() {
try {
Thread.sleep(5000); // 5 seconds
} catch (InterruptedException ie) {
System.out.println("Interrupted, exiting");
// perform whatever cleanup is needed
}
}
});
t.start();
try { Thread.sleep(1000); } catch (InterruptedException ie) {}
t.interrupt();
}
}
I'm mess here, interrupt it not same as thread stopping, isn't it?
Seeing as how the run method will finish as soon as the catch block is done, yes it is. That's the trick. You need to interrupt the thread and let it clean itself up and exit normally.
That's what I want to get from yourself. Because our thread starter can be confused. ;)
If the thread isn't calling a method that is interruptable when the interrupt is sent, then catch block will never be called.The thread will have to test to see if it has been interrupted and then handle that in normal code, not in a catch block.
Sleep is itself interruptable. Try it out and tell me how long the program sleeps and what it outputs.
Nevermind. I read your post wrong.
It was, obviously, only meant to be a simple example.
An example of the other way
And, BTW, there's no reason why the methods can't be combined (directed at the OP, not you Norm).Code:public class Bogus2 {
public static void main (String[] args) {
Thread t = new Thread(
new Runnable() {
public void run() {
int i = 0;
while (!Thread.interrupted()) {
System.out.println(i++);
}
}
});
t.start();
try { Thread.sleep(50); } catch (InterruptedException ie) {}
t.interrupt();
}
}
public class Bogus2 {
public static void main (String[] args) {
Thread t = new Thread(
new Runnable() {
public void run() {
int i = 0;
while (!Thread.interrupted()) {
System.out.println(i++);
}
}
});
t.start();
try { Thread.sleep(50); } catch (InterruptedException ie) {}
t.interrupt();
}
}
isnt the above code just for one thread running.
hello i am a student and a beginner learning MT so my apologies in advance if my questions are not clear.
so what if we have array of threads and there are concurrent threads running and after some times of processing later we dont need one of the thread, is there a way to kill it.
since i am doing a client/server app is it that the thread dies if the client disconnects.
In that example two threads are running. The original thread, and another one that it started. There is no reason why you can't start more than one additional thread. You need to call the interuppt on the right thread, however. And, no, aside from interrupt (or closing whatever streams it might be reading from) there is no safe way to kill a thread (and for anyone who wants to bring up setting a flag/variable and checking that variable from time to time, this is exactly what interrupt and interrupted do, there is no reason to "roll your own" when it already exists). The stop method exists, but it is deprecated and strongly discouraged.
Also, if your using a Thread per client, and the client disconnects, your design should be smart enough for the thread servicing that client to recognise that (eventually) and then finish it's work and simply end.
thanks a lot im starting to get the hang of it.
Denis