Possible Duplicate:
Java Thread Garbage collected or not
Consider the following class:
class Foo implements Runnable {
public Foo () {
Thread th = new Thread (this);
th.start();
}
public run() {
... // long task
}
}
If we create several instances of Foo
by doing
new Foo();
new Foo();
new Foo();
new Foo();
(note that we don't keep a pointer to them).
Could those instances be removed by the garbage collector before the thread in
run()
ends? (In other words: is there any reference to theFoo
objects?)And, in the other hand, will those instances be removed by the GC after the thread in `run()' ends, or are we wasting memory ("memory leak")?
If either 1. or 2. are a problem, what's the right way to do it?
Thanks