Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Kill a thread automatically

Reply
Thread Tools

Kill a thread automatically

 
 
Adi Schwarz
Guest
Posts: n/a
 
      01-30-2004
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

 
Reply With Quote
 
 
 
 
Niki Estner
Guest
Posts: n/a
 
      01-30-2004
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" <(E-Mail Removed)> wrote in
news:bvdqac$r0bqf$(E-Mail Removed)-berlin.de...
> 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
>



 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      01-30-2004
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
 
Reply With Quote
 
Adi Schwarz
Guest
Posts: n/a
 
      01-30-2004
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

 
Reply With Quote
 
Bryan Bullard
Guest
Posts: n/a
 
      01-30-2004

"Adi Schwarz" <(E-Mail Removed)> wrote in message
news:bvdqac$r0bqf$(E-Mail Removed)-berlin.de...
> 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
>



 
Reply With Quote
 
Robert Olofsson
Guest
Posts: n/a
 
      01-30-2004
Niki Estner <(E-Mail Removed)> 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
 
Reply With Quote
 
Niki Estner
Guest
Posts: n/a
 
      01-30-2004
"Robert Olofsson" <(E-Mail Removed)> wrote in
news:bvef9n$2th$(E-Mail Removed)...
> Niki Estner <(E-Mail Removed)> 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



 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      01-31-2004

"Adi Schwarz" <(E-Mail Removed)> wrote in message
news:bvdqac$r0bqf$(E-Mail Removed)-berlin.de...
> 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/tutor...ntial/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)


 
Reply With Quote
 
S Manohar
Guest
Posts: n/a
 
      02-01-2004
"Tony Morris" <(E-Mail Removed)> wrote in message news:<401b6734$0$3128$(E-Mail Removed) u>...
> "Adi Schwarz" <(E-Mail Removed)> wrote in message
> news:bvdqac$r0bqf$(E-Mail Removed)-berlin.de...
> > 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?
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      02-02-2004
Niki Estner wrote:

> "Robert Olofsson" <(E-Mail Removed)> wrote in
> news:bvef9n$2th$(E-Mail Removed)...
>
>>Niki Estner <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread#raise, Thread#kill, and timeout.rb are unsafe Charles Oliver Nutter Ruby 43 03-25-2008 02:31 PM
KILL BABY KILL widescreen drsd2kill DVD Video 3 11-29-2004 09:36 PM
Bava's KILL BABY KILL widescreen drsd2kill DVD Video 0 11-27-2004 12:04 AM
thread, threading; how to kill a thread? Jerry Sievers Python 12 11-19-2004 12:10 AM
Anyway to Kill virus email on my Host Mailbox automatically? Dennis@NoSpam.com Perl Misc 2 08-22-2003 03:38 AM



Advertisments