Stopping a ThreadGroup

T

Timo Nentwig

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???
 
S

Steve Horsley

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

Timo Nentwig

Steve said:
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());
}
}
 
X

xarax

Timo Nentwig said:
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!
 
G

Gambit

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top