|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

09-25-2008, 10:36 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
Thread killing
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
|
|

09-25-2008, 11:07 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-25-2008, 01:02 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
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();
}
}
|
|

09-25-2008, 01:11 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
I'm mess here, interrupt it not same as thread stopping, isn't it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-25-2008, 01:33 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
|
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.
|
|

09-25-2008, 01:46 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
That's what I want to get from yourself. Because our thread starter can be confused. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-25-2008, 06:21 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

09-25-2008, 08:04 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
|
Sleep is itself interruptable. Try it out and tell me how long the program sleeps and what it outputs.
|
|

09-25-2008, 08:19 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
|
Nevermind. I read your post wrong.
It was, obviously, only meant to be a simple example.
Last edited by masijade : 09-25-2008 at 08:24 PM.
|
|

09-25-2008, 08:38 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
An example of the other way
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();
}
}
And, BTW, there's no reason why the methods can't be combined (directed at the OP, not you Norm).
Last edited by masijade : 09-25-2008 at 08:51 PM.
Reason: Made it simpler
|
|

09-25-2008, 09:55 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
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.
|
|

09-25-2008, 10:09 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
|
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.
Last edited by masijade : 09-25-2008 at 10:12 PM.
|
|

09-25-2008, 10:14 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
Originally Posted by Norm
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.
One more point, the test must be in normal code, but the cleanup can still be in a catch block. Simply throw an InterruptedException when interrupted() returns true.
|
|

09-25-2008, 11:28 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
thanks a lot im starting to get the hang of it.
Denis
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|