Destroying a thread in a threadgroup

Discussion in 'Java' started by Frank Cisco, Jun 2, 2009.

  1. Frank Cisco

    Frank Cisco Guest

    How do you destroy a thread in a ThreadGroup? What I want to do is put a
    timer on each thread and if any don't return in say 30 seconds then destroy
    the thread. I've tried adding a seperate timer thread to each running thread
    but it's too memory intensive. Also the ThreadGroup won't allow direct
    access to it's threads - it seems to make a copy of the threads?
     
    Frank Cisco, Jun 2, 2009
    #1
    1. Advertising

  2. Frank Cisco

    Mark Space Guest

    Frank Cisco wrote:
    > How do you destroy a thread in a ThreadGroup? What I want to do is put a
    > timer on each thread and if any don't return in say 30 seconds then destroy
    > the thread. I've tried adding a seperate timer thread to each running thread
    > but it's too memory intensive. Also the ThreadGroup won't allow direct
    > access to it's threads - it seems to make a copy of the threads?
    >
    >


    Who writes the thread code? You or someone else?

    If it's you, you can control what happens when the thread is running and
    you can therefore be assured what will happen when you call a method to
    stop it. Thread stop() works, but is deprecated. I think however it
    would be ok to call Thread stop() if you knew there were no resources
    that would leak as a result.

    Setting a flag or calling Thread interrupt() are less intrusive ways to
    stop a thread, especially if you are dealing with IO (which would leak
    resources if you call Thread stop()).

    The Executor service mentioned by Zig just uses interrupt() or waits for
    a thread to terminate on it's own, there's no magic there. An Executor
    will not forcibly stop a thread due to the potential for resource leaks.
    You have to write your thread code to respond to an interrupt(), or
    the thread will always run to completion.
     
    Mark Space, Jun 2, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig

    Stopping a ThreadGroup

    Timo Nentwig, Oct 25, 2003, in forum: Java
    Replies:
    4
    Views:
    2,829
    Gambit
    Oct 25, 2003
  2. avinashrk
    Replies:
    10
    Views:
    4,766
    Andrew Thompson
    Dec 14, 2004
  3. Replies:
    1
    Views:
    387
  4. Jason Cavett

    Destroying a Thread

    Jason Cavett, Apr 3, 2007, in forum: Java
    Replies:
    4
    Views:
    381
    Knute Johnson
    Apr 3, 2007
  5. Crouchez

    Threads in a threadgroup

    Crouchez, Nov 6, 2007, in forum: Java
    Replies:
    3
    Views:
    330
    Crouchez
    Nov 6, 2007
Loading...

Share This Page