Destroying a thread in a threadgroup

F

Frank Cisco

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

Mark Space

Frank said:
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.
 

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,817
Latest member
DicWeils

Latest Threads

Top