Results 1 to 19 of 19
Thread: Thread Stop() method not working
- 10-03-2009, 10:03 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
Thread Stop() method not working
Hi experts,
In my case i have a main method from which i spawn a chid thread , and i am making my main to halt it execution till chid thread is done using join() method. Also i specify timeout in join method, so that if child thread doesnt complete in given timeout duration , I will kill the child thread.
The issue is , sometimes it works but some times the thread is not killed ? any idea why ?
Beow is the piece of code..
public static void main(String args[])
{
t = new Thread(c);
t.start();
// timeout in mins - conversion to millisec
long timeout = jobTimeOut*60000;
t.join(timeout);
if(t.getState() != Thread.State.TERMINATED){
t.stop();
job_timeout_status_flag = true;
}
}
Regards
- 10-03-2009, 10:41 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Because "stop" is deprecated because it is (in program terms) very dangerous.
- 10-03-2009, 11:51 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
yupp, i kmow its deprecated and i am using it in jdk 1.5 !
Still , its deprecated because its not safe to kill threads abruptly , but u can always kill threads if you can make sure that the consequences of killing thread is takencare.!
Stop method as per documentation should kill no matter what...am i not right?
- 10-03-2009, 01:13 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
If it is in a place where it can't be interrupted, no.
And believe what you want, but it is an extremely bad practice to use it, and you may think that you have all the "consequences" taken care of, but considering your Heap errors, I'm assuming you don't, and most likely can't.
- 10-03-2009, 01:24 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
ok you tell me how i can get this done without stop() method..
please note that i cant use interrupt in my case and also,,,,,i cant check flags and exit run method..none of the both is right solution for me,,,,is there any other way to kill the invoked job if its not coming out of execution in the specified timeout interval....
- 10-03-2009, 01:30 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Then you need to redesign your "run" tasks to properly handle interuppts.
If these tasks are intended to be run as a thread they should already do so. And if they are not intended to be run as threads then you are going to have to go with a fire and forget type of mentality. Simply remove all references to it, and, if you can get at them, close it's resources (which should also cause it to "abort") and let it go.
- 10-03-2009, 01:46 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
ok..say suppose...i have
public void run(){
performXYZOperations();
}
and i dunt have control over
performXYZOperations() , and if somehow if i manage to comeout of run through interuppt...then does that mean ....performXYZOperations() is also terminated and resources occupied by it are eligible for gc?
- 10-03-2009, 01:52 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
If you simply interrupt (or stop/destroy/suspend) a thread with open resources, and the "performXYZ" process is not properly designed to cleanup those resources, then the thread and most of it's objects will be eligable for GC, but those open resources will cause problems.
- 10-03-2009, 01:57 PM #9
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
yes,,,the performXYZ() operation is no designed by me ..we just provide the framework on which the third person host his jobs....so i dunt have any knowledge on what his jobs does...
that is the issue....and thats why a dedicate dthread is allowed for a job and when completion of struck job , the thread is supposed to be killed and also need to get the recources occupied by job need to be cleaned up by framework...
since u told stop has memory leak issues....whaty other ways are there to get this done?
- 10-03-2009, 01:59 PM #10
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
yes,,,the performXYZ() operation is no designed by me ..we just provide the framework on which the third person host his jobs....so i dunt have any knowledge on what his jobs does...
that is the issue....and thats why a dedicate dthread is allowed for a job and when completion of job or struck job , the thread is supposed to be killed and also need to get the recources occupied by job need to be cleaned up by framework...
since u told stop has memory leak issues....whaty other ways are there to get this done?
Add to charan reddy's Reputation
- 10-03-2009, 02:01 PM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Is the "client" delivering a "main" class? If not, design your own "main" class to wrap it, and set it off as a separate process using ProcessBuilder and then use Process.destroy().
But make sure to handle the Streams properly see http://www.javaworld.com/javaworld/j...229-traps.html and read it thoroughly and completely.
- 10-03-2009, 02:03 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Put a boolean flag in your thread and make your thread's run method be controlled by a while loop that exits when the flag is changed. If you want to stop your thread just change the value of that flag.
Last edited by r035198x; 10-03-2009 at 02:06 PM.
- 10-03-2009, 02:04 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
See http://www.java-forums.org/advanced-...-new-post.html
This is a very disjointed conversation at the moment.
- 10-03-2009, 02:08 PM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Oh dear. It's one of those ...
- 10-03-2009, 02:11 PM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Yes, it is. ;-)
- 10-03-2009, 02:51 PM #16
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
well may i know which one of those.. :)
-
Original Poster: please chose one thread of the two as the active one and I will lock the other one. Otherwise I may have to lock both. In the future, please post a question in the single best forum for the question. Thank you for your cooperation.
- 10-03-2009, 02:59 PM #18
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
ok u can mark this as solved
-
Thanks. I will lock this thread. The link to the other one is in masijade's post #13.
Similar Threads
-
Forcing a thread to stop
By sukatoa in forum Threads and SynchronizationReplies: 7Last Post: 07-17-2009, 06:41 AM -
Help to stop a thread
By raghu_lzybns in forum New To JavaReplies: 4Last Post: 07-09-2009, 04:39 PM -
[SOLVED] How to stop a thread
By AlejandroPe in forum New To JavaReplies: 5Last Post: 04-29-2009, 03:05 PM -
how to stop a thread
By willemjav in forum Advanced JavaReplies: 19Last Post: 09-10-2008, 07:11 AM -
The safe way to stop a thread
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:31 PM


LinkBack URL
About LinkBacks

Bookmarks