Threads and Garbage Collection

Discussion in 'Java' started by Andrew Mallinson, Jan 30, 2004.

  1. Hey

    Ive currently got several threads running in my program that collect data on
    how the rest of the program is performing. I just want to make sure that
    once these results collection threads have finished ie. got to the end of
    their run() method - that it will always still be possible to call their
    other methods to access the data.

    That is the theads wont be garbage collected until all references to them
    within the program are gone.

    Also as a side note - is it possible to restart a thread by calling <thread
    object name>.start(); - it didnt seem so when I tried it but this was only a
    brief test and no error was generated.

    Thanks in advance for your help
    Regards
    Andrew Mallinson
    Andrew Mallinson, Jan 30, 2004
    #1
    1. Advertising

  2. Andrew Mallinson

    BarryNL Guest

    Andrew Mallinson wrote:
    > Hey
    >
    > Ive currently got several threads running in my program that collect data on
    > how the rest of the program is performing. I just want to make sure that
    > once these results collection threads have finished ie. got to the end of
    > their run() method - that it will always still be possible to call their
    > other methods to access the data.
    >
    > That is the theads wont be garbage collected until all references to them
    > within the program are gone.


    The objects *won't* be GC'd until all references to them are gone. It
    doesn't matter if they're active objects or not, it's the references
    that matter.

    > Also as a side note - is it possible to restart a thread by calling <thread
    > object name>.start(); - it didnt seem so when I tried it but this was only a
    > brief test and no error was generated.


    Runnable myRunner = new Runnable() {
    public void run() {
    // do stuff
    }
    };
    Thread myThread = new Thread(myRunner).start();
    // do more stuff
    if(!myThread.isAlive()) { // has thread finished?
    myThread = new Thread(myRunner).start();
    }

    .... is this what you mean? Not restarting a thread, but does start a new
    thread using the same original Runnable object - which is basically the
    same thing.

    >
    > Thanks in advance for your help
    > Regards
    > Andrew Mallinson
    BarryNL, Jan 30, 2004
    #2
    1. Advertising

  3. > > Also as a side note - is it possible to restart a thread by calling
    <thread
    > > object name>.start(); - it didnt seem so when I tried it but this was

    only a
    > > brief test and no error was generated.

    >
    > Runnable myRunner = new Runnable() {
    > public void run() {
    > // do stuff
    > }
    > };
    > Thread myThread = new Thread(myRunner).start();
    > // do more stuff
    > if(!myThread.isAlive()) { // has thread finished?
    > myThread = new Thread(myRunner).start();
    > }
    >
    > ... is this what you mean? Not restarting a thread, but does start a new
    > thread using the same original Runnable object - which is basically the
    > same thing.


    Yes it is - thanks for your response
    its the same object's run method which is restarting (which is what i
    wanted) but using a new thread. - so the actual thread isnt restarted.
    Andrew Mallinson, Jan 30, 2004
    #3
    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. Kevin Jackson
    Replies:
    1
    Views:
    409
    Alvin Bruney [MVP]
    May 28, 2004
  2. Shirish Rai

    Garbage Collection and Threads

    Shirish Rai, Jun 9, 2004, in forum: Java
    Replies:
    3
    Views:
    344
    Chris Smith
    Jun 9, 2004
  3. Replies:
    8
    Views:
    9,625
    DrChaos
    Nov 17, 2005
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    958
    Øyvind Isaksen
    May 18, 2007
  5. Brian Candler

    Threads preventing garbage collection?

    Brian Candler, Sep 17, 2008, in forum: Ruby
    Replies:
    11
    Views:
    202
    ara.t.howard
    Sep 18, 2008
Loading...

Share This Page