Threads and Garbage Collection

A

Andrew Mallinson

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
 
B

BarryNL

Andrew said:
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.
 
A

Andrew Mallinson

Also as a side note - is it possible to restart a thread by calling
<thread
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.
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top