Kill a thread automatically

A

Adi Schwarz

Hi,

If I have a class that runs a thread and no reference points to it, the
gc won't finalize the class because the thread is still running -> so
stopping the thread in the finalize() method won't work. Afaik this is
also true for daemon threads.

Is it possible to stop a thread automatically when there are no
references left? In my case it does not matter if the thread is
gracefully interrupted or just killed and whiped out.

greets,
-adi
 
N

Niki Estner

Couldn't you create some class that encapsulates your thread; All the code
outside the thread uses references to this wrapper object, but the thread
itself has no reference (or only weak ones) to its wrapper. If the wrapper
gets finalized, it could kill the thread. (I didn't really think this
through, but I guess it should work)

Niki
 
C

Chris Smith

Adi said:
If I have a class that runs a thread and no reference points to it, the
gc won't finalize the class because the thread is still running -> so
stopping the thread in the finalize() method won't work. Afaik this is
also true for daemon threads.

Is it possible to stop a thread automatically when there are no
references left? In my case it does not matter if the thread is
gracefully interrupted or just killed and whiped out.

No, this is not possible. You're going to have to manage access to the
thread and explicitly signal it to finish when no one is interested in
the work it's doing.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Adi Schwarz

Niki said:
Couldn't you create some class that encapsulates your thread; All the code
outside the thread uses references to this wrapper object, but the thread
itself has no reference (or only weak ones) to its wrapper. If the wrapper
gets finalized, it could kill the thread. (I didn't really think this
through, but I guess it should work)

thanks for the hint to weak references!

-as
 
B

Bryan Bullard

Adi Schwarz said:
Hi,

If I have a class that runs a thread and no reference points to it, the
gc won't finalize the class because the thread is still running -> so
stopping the thread in the finalize() method won't work. Afaik this is
also true for daemon threads.

Is it possible to stop a thread automatically when there are no
references left?

yes, just let the thread end normally (e.g., when Thread.run() returns).
if this is completely impractical, you might need to rethink your design.
 
R

Robert Olofsson

: Couldn't you create some class that encapsulates your thread; All the code
: outside the thread uses references to this wrapper object, but the thread
: itself has no reference (or only weak ones) to its wrapper. If the wrapper
: gets finalized, it could kill the thread. (I didn't really think this
: through, but I guess it should work)

This wont work.
Threads have references to them, the ThreadGroup they are part of will
still have a reference. Since you have a reference to the thread it
wont die until you tell it to or it exits the run method.

This means that you will have to store the thread somewhere so that
you can kill it if needed.

/robo
 
N

Niki Estner

Robert Olofsson said:
: Couldn't you create some class that encapsulates your thread; All the code
: outside the thread uses references to this wrapper object, but the thread
: itself has no reference (or only weak ones) to its wrapper. If the wrapper
: gets finalized, it could kill the thread. (I didn't really think this
: through, but I guess it should work)

This wont work.
Threads have references to them, the ThreadGroup they are part of will
still have a reference. Since you have a reference to the thread it
wont die until you tell it to or it exits the run method.

I guess you didn't read my post, did you?
Of course there is a reference to the Thread object as long as the thread is
running.
But if the thread doesn't have a reference to its wrapper object (or only a
weak reference), the wrapper object will be destroyed by the GC like any
other object, too. Now, the wrapper object can kill the thread (or tell it
to exit) when it's finalize method gets called.
This means that you will have to store the thread somewhere so that
you can kill it if needed.

Of course. The wrapper object must contain a reference to the thread object,
so it can kill it in the Finalize method.
 
T

Tony Morris

Adi Schwarz said:
Hi,

If I have a class that runs a thread and no reference points to it, the
gc won't finalize the class because the thread is still running -> so
stopping the thread in the finalize() method won't work. Afaik this is
also true for daemon threads.

Is it possible to stop a thread automatically when there are no
references left? In my case it does not matter if the thread is
gracefully interrupted or just killed and whiped out.

greets,
-adi

Relying on a call to finalize() is bad practice, since it not guaranteed
that the call will occur.
It is guaranteed that the call will occur if the object is garbage
collected, but the object is not guaranteed to be garbage collected.

Threads cease execution at the end of the run() method. You can interrupt()
a Thread if it is in a state where an interrupt might occur (generally sleep
state), although there's a bit more to it than just that - I suggest you
read the threads tutorial. Forcefully terminating a Thread with stop() is
deprecated for good reason.

http://java.sun.com/docs/books/tutorial/essential/threads/
That might clarify it a bit.


--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
Home : +61 7 5502 7987
Work : +61 7 5552 4076
Mobile : 0408 711 099
(2003 VTR1000F)
 
S

S Manohar

If there were an automatic way of doing this, it would be pretty
dangerous!

I think the best way to do this is for the thread itself to stop
itself by returning from the run() method. Somewhere in the thread's
loop, it should check a flag, and die if needed.
Perhaps, each time you create or drop a reference to the thread, you
could increment or decrement a counter?
 
J

John C. Bollinger

Niki said:
I guess you didn't read my post, did you?
Of course there is a reference to the Thread object as long as the thread is
running.
But if the thread doesn't have a reference to its wrapper object (or only a
weak reference), the wrapper object will be destroyed by the GC like any
other object, too. Now, the wrapper object can kill the thread (or tell it
to exit) when it's finalize method gets called.




Of course. The wrapper object must contain a reference to the thread object,
so it can kill it in the Finalize method.

Except you cannot count on any object's finalize method _ever_ being
invoked. You can certainly create an object that knows how to terminate
the thread, but you still have to explicitly tell it to do so -- in
other words, your suggestion just pushes the problem up one level.


John Bollinger
(e-mail address removed)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top