本文介绍了Java:我需要从垃圾收集器保护一个线程对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

是Thread对象是GC的候选者吗? 如果它已经启动,那么它不符合GC的要求 - 正在运行的代码可以要求 Thread。 currentThread(),毕竟。



如果您刚创建它,但没有启动它,像这样:

  {
Thread pointless = new Thread();
}

然后我怀疑它会符合GC的条件 - 但创建一个线程而不启动它是非常不寻常的。 (我想在你开始之前可能会抛出异常......)

After

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

is the Thread object a candidate for the GC?

解决方案

If it's started, it's not eligible for GC - the code that's running can ask for Thread.currentThread(), after all.

If you just created it but didn't start it, like this:

{
    Thread pointless = new Thread();
}

then I suspect it would be eligible for GC - but it's pretty unusual to create a thread without starting it. (I guess an exception could be thrown before you got round to starting it...)

这篇关于Java:我需要从垃圾收集器保护一个线程对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:12