Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-25-2008, 10:36 AM
Member
 
Join Date: Sep 2008
Posts: 4
denis is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-25-2008, 11:07 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-25-2008, 01:02 PM
Senior Member
 
Join Date: Jun 2008
Posts: 481
masijade is on a distinguished road
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-25-2008, 01:11 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-25-2008, 01:33 PM
Senior Member
 
Join Date: Jun 2008
Posts: 481
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-25-2008, 01:46 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-25-2008, 06:21 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-25-2008, 08:04 PM
Senior Member
 
Join Date: Jun 2008
Posts: 481
masijade is on a distinguished road
Sleep is itself interruptable. Try it out and tell me how long the program sleeps and what it outputs.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-25-2008, 08:19 PM
Senior Member
 
Join Date: Jun 2008
Posts: 481
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-25-2008, 08:38 PM
Senior Member
 
Join Date: Jun 2008
Posts: 481
masijade is on a distinguished road
An example of the other way
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(); } }
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 09-25-2008, 09:55 PM
Member
 
Join Date: Sep 2008
Posts: 4
denis is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 09-25-2008, 10:09 PM
Senior Member
 
Join Date: Jun 2008
Posts: 481
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 09-25-2008, 10:14 PM
Senior Member
 
Join Date: Jun 2008
Posts: 481
masijade is on a distinguished road
Quote:
Originally Posted by Norm View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 09-25-2008, 11:28 PM
Member
 
Join Date: Sep 2008
Posts: 4
denis is on a distinguished road
thanks a lot im starting to get the hang of it.
Denis
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how high-priority thread allow other thread rameshkr Threads and Synchronization 3 09-10-2008 05:16 PM
passing a value from parent thread to child thread sachinj13 Threads and Synchronization 7 09-07-2008 11:06 PM
data from the main/GUI thread to another runnin thread... cornercuttin Threads and Synchronization 2 04-24-2008 12:30 AM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 09:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 11:31 AM


All times are GMT +3. The time now is 08:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org