Kill a thread automatically

Discussion in 'Java' started by Adi Schwarz, Jan 30, 2004.

  1. Adi Schwarz

    Adi Schwarz Guest

    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
     
    Adi Schwarz, Jan 30, 2004
    #1
    1. Advertising

  2. Adi Schwarz

    Niki Estner Guest

    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" <> wrote in
    news:bvdqac$r0bqf$-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
    >
     
    Niki Estner, Jan 30, 2004
    #2
    1. Advertising

  3. Adi Schwarz

    Chris Smith Guest

    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
     
    Chris Smith, Jan 30, 2004
    #3
  4. Adi Schwarz

    Adi Schwarz Guest

    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
     
    Adi Schwarz, Jan 30, 2004
    #4
  5. "Adi Schwarz" <> wrote in message
    news:bvdqac$r0bqf$-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
    >
     
    Bryan Bullard, Jan 30, 2004
    #5
  6. 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)

    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
     
    Robert Olofsson, Jan 30, 2004
    #6
  7. Adi Schwarz

    Niki Estner Guest

    "Robert Olofsson" <> wrote in
    news:bvef9n$2th$...
    > 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)
    >
    > 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
     
    Niki Estner, Jan 30, 2004
    #7
  8. Adi Schwarz

    Tony Morris Guest

    "Adi Schwarz" <> wrote in message
    news:bvdqac$r0bqf$-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/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)
     
    Tony Morris, Jan 31, 2004
    #8
  9. Adi Schwarz

    S Manohar Guest

    "Tony Morris" <> wrote in message news:<401b6734$0$3128$>...
    > "Adi Schwarz" <> wrote in message
    > news:bvdqac$r0bqf$-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?
     
    S Manohar, Feb 1, 2004
    #9
  10. Niki Estner wrote:

    > "Robert Olofsson" <> wrote in
    > news:bvef9n$2th$...
    >
    >>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)
    >>
    >>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
     
    John C. Bollinger, Feb 2, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chintan jajal via .NET 247

    Delegate Endinvoke Kill thread

    chintan jajal via .NET 247, May 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,994
    Richard Grimes [MVP]
    Jun 6, 2005
  2. Rasmus B. Nielsen

    thread kill ?

    Rasmus B. Nielsen, Aug 30, 2003, in forum: Java
    Replies:
    0
    Views:
    493
    Rasmus B. Nielsen
    Aug 30, 2003
  3. Jerry Sievers

    thread, threading; how to kill a thread?

    Jerry Sievers, Nov 17, 2004, in forum: Python
    Replies:
    12
    Views:
    1,186
    Mustafa Demirhan
    Nov 19, 2004
  4. Charles Oliver Nutter
    Replies:
    43
    Views:
    666
    Roger Pack
    Mar 25, 2008
  5. Replies:
    2
    Views:
    144
    David Efflandt
    Aug 22, 2003
Loading...

Share This Page