Java: do I need to protect the Thread object from the garbage collector?

After

{
Thread t = new Thread();
t.start();
}

Is the Thread object a candidate for the GC?

+3
source share
3 answers

If it is running, it is not suitable for the GC - code that works may request Thread.currentThread(), in the end.

If you just created it but did not start it, follow these steps:

{
    Thread pointless = new Thread();
}

then I suspect that he will be entitled to the GC - but it’s rather unusual to create a thread without starting it. (I assume that the exception could be thrown before you got it to start it ...)

+5
source

, . ( t.start( )), GC.

, , - thread.isAlive().

final boolean isAlive( )

isAlive( ) true, , , . false. t.isAlive(), , .

(, Jon Code), GC.

+6

You only need to protect Thread if you want to save it after its completion. It cannot be GC'ed during its operation (or anything that uses Thread)

0
source

All Articles