Kill a thread automatically  
Author Message
Adi Schwarz





PostPosted: 2004-1-30 22:40:00 Top

java-programmer, Kill a thread automatically 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

 
Niki Estner





PostPosted: 2004-1-30 23:01:00 Top

java-programmer >> Kill a thread automatically 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

"Adi Schwarz" <email***@***.com> wrote in
news:bvdqac$r0bqf$email***@***.com...
> 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
>


 
Chris Smith





PostPosted: 2004-1-30 23:02:00 Top

java-programmer >> Kill a thread automatically Adi Schwarz wrote:
> 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
 
 
Adi Schwarz





PostPosted: 2004-1-30 23:23:00 Top

java-programmer >> Kill a thread automatically Niki Estner wrote:

> 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

 
 
Bryan Bullard





PostPosted: 2004-1-31 0:29:00 Top

java-programmer >> Kill a thread automatically
"Adi Schwarz" <email***@***.com> wrote in message
news:bvdqac$r0bqf$email***@***.com...
> 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.

>In my case it does not matter if the thread is
> gracefully interrupted or just killed and whiped out.
>
> greets,
> -adi
>


 
 
Robert Olofsson





PostPosted: 2004-1-31 4:37:00 Top

java-programmer >> Kill a thread automatically Niki Estner <email***@***.com> wrote:
: 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
 
 
Niki Estner





PostPosted: 2004-1-31 6:18:00 Top

java-programmer >> Kill a thread automatically "Robert Olofsson" <email***@***.com> wrote in
news:bvef9n$2th$email***@***.com...
> Niki Estner <email***@***.com> wrote:
> : 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.

>
> /robo


 
 
Tony Morris





PostPosted: 2004-1-31 16:29:00 Top

java-programmer >> Kill a thread automatically
"Adi Schwarz" <email***@***.com> wrote in message
news:bvdqac$r0bqf$email***@***.com...
> 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)


 
 
sgmanohar





PostPosted: 2004-2-1 8:06:00 Top

java-programmer >> Kill a thread automatically "Tony Morris" <email***@***.com> wrote in message news:<401b6734$0$3128$email***@***.com>...
> "Adi Schwarz" <email***@***.com> wrote in message
> news:bvdqac$r0bqf$email***@***.com...
> > 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
> >
>
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?
 
 
John C. Bollinger





PostPosted: 2004-2-3 5:22:00 Top

java-programmer >> Kill a thread automatically Niki Estner wrote:

> "Robert Olofsson" <email***@***.com> wrote in
> news:bvef9n$2th$email***@***.com...
>
>>Niki Estner <email***@***.com> wrote:
>>: 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.

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
email***@***.com