Stopping a ThreadGroup

Discussion in 'Java' started by Timo Nentwig, Oct 25, 2003.

  1. Timo Nentwig

    Timo Nentwig Guest

    Hi!

    I've a ThreadGroup with several threads..so, since ThreadGroup.stop() is as
    insecure as Thread.stop() how do I stop all threads in a ThreadGroup().

    I actually override all Thread's stop method:

    public void stop()
    {
    thread = null;
    }

    So, ThreadGroup.stop() should call all thread's stop() method which is just
    safe, isn't it?

    Do I have to do this manually?! I.e. enumerate() all threads in a
    ThreadGroup(), invoking their stop() and destroy()ing the ThreadGroup after
    that???
    Timo Nentwig, Oct 25, 2003
    #1
    1. Advertising

  2. On Sat, 25 Oct 2003 13:07:12 +0200, Timo Nentwig wrote:

    > Hi!
    >
    > I've a ThreadGroup with several threads..so, since ThreadGroup.stop() is as
    > insecure as Thread.stop() how do I stop all threads in a ThreadGroup().
    >
    > I actually override all Thread's stop method:
    >
    > public void stop()
    > {
    > thread = null;
    > }


    I really don't see what that is supposed to achieve. Actually, I didn't
    know

    >
    > So, ThreadGroup.stop() should call all thread's stop() method which is just
    > safe, isn't it?
    >
    > Do I have to do this manually?! I.e. enumerate() all threads in a
    > ThreadGroup(), invoking their stop() and destroy()ing the ThreadGroup after
    > that???


    Since Thread.stop() does nothing, I guess ThreadGroup.stop() also does
    nothing - even if it calls all the individual Thread.stop() methids, it
    will still end up doing nothing.

    Read the javadocs for Thread.stop(). It just isn't safe, which is why it
    does nothing these days.

    The answer is to set a flag somewhere, and have the thread(s) check this
    flag on a regular basis. Than they can clean up if required before they
    exit. The flag should be a volatile variable, or the return value from a
    synchronized method. I believe that Thread.interrupt() can be used for
    this, and also has the advantage that it can prematurely terminate blocked
    I/O and wait states.

    If you google this newsgroup for "how stop thread", you will find many
    discussions of this subject.

    Steve


    Steve.
    Steve Horsley, Oct 25, 2003
    #2
    1. Advertising

  3. Timo Nentwig

    Timo Nentwig Guest

    Steve Horsley wrote:
    > The answer is to set a flag somewhere, and have the thread(s) check this
    > flag on a regular basis. Than they can clean up if required before they


    So, what is a ThreadGroup actually good for?

    Unfortnately I cannot use the flag solution:

    public void run()
    {
    try
    {
    final ObjectInputStream ois = new
    ObjectInputStream(client.getInputStream());

    while (fuckTheFlag)
    {
    blah(ois.readObject());
    }
    }
    Timo Nentwig, Oct 25, 2003
    #3
  4. Timo Nentwig

    xarax Guest

    "Timo Nentwig" <> wrote in message
    news:bne1r5$ti8o8$-berlin.de...
    > Steve Horsley wrote:
    > > The answer is to set a flag somewhere, and have the thread(s) check this
    > > flag on a regular basis. Than they can clean up if required before they

    >
    > So, what is a ThreadGroup actually good for?

    /snip/

    You can call threadGroup.interrupt() to send an interrupt
    to all of the threads.

    If a thread is stuck on a blocking I/O call, then
    you may be able to close asynchronously the stream
    to throw an IOException. This works with socket
    channels, but unsure about "ordinary" file streams.


    --
    ----------------------------------------------
    Jeffrey D. Smith
    Farsight Systems Corporation
    24 BURLINGTON DRIVE
    LONGMONT, CO 80501-6906
    http://www.farsight-systems.com
    z/Debug debugs your Systems/C programs running on IBM z/OS!
    xarax, Oct 25, 2003
    #4
  5. Timo Nentwig

    Gambit Guest

    Good advice: Don't use ThreadGroup: "Thread groups are best viewed as an
    unsuccessful experiment, and you may simply ignore their existence." (Joshua
    Bloch, the software architect at Sun).
    Read Concurrency chapter in Thinking in Java 3rd edition (
    www.BruceEckel.com ) for more info.

    Cheers,
    Gambit
    Gambit, Oct 25, 2003
    #5
    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. avinashrk
    Replies:
    10
    Views:
    4,706
    Andrew Thompson
    Dec 14, 2004
  2. Replies:
    1
    Views:
    373
  3. Crouchez

    Threads in a threadgroup

    Crouchez, Nov 6, 2007, in forum: Java
    Replies:
    3
    Views:
    320
    Crouchez
    Nov 6, 2007
  4. Frank Cisco

    Destroying a thread in a threadgroup

    Frank Cisco, Jun 2, 2009, in forum: Java
    Replies:
    1
    Views:
    380
    Mark Space
    Jun 2, 2009
  5. Dave Thomas

    ThreadGroup#enclose

    Dave Thomas, Aug 5, 2003, in forum: Ruby
    Replies:
    2
    Views:
    91
    Dave Thomas
    Aug 5, 2003
Loading...

Share This Page