Results 1 to 19 of 19
  1. #1
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Question 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

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because "stop" is deprecated because it is (in program terms) very dangerous.

  3. #3
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    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?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  5. #5
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    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....

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  7. #7
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    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?

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  9. #9
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    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. #10
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    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

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See http://www.java-forums.org/advanced-...-new-post.html

    This is a very disjointed conversation at the moment.

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Oh dear. It's one of those ...

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes, it is. ;-)

  16. #16
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    well may i know which one of those.. :)

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  18. #18
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    ok u can mark this as solved

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Forcing a thread to stop
    By sukatoa in forum Threads and Synchronization
    Replies: 7
    Last Post: 07-17-2009, 06:41 AM
  2. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 04:39 PM
  3. [SOLVED] How to stop a thread
    By AlejandroPe in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 03:05 PM
  4. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 AM
  5. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •