Java线程设置为null时会发生什么? [英] What will happen when a Java thread is set to null?

查看:679
本文介绍了Java线程设置为null时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程启动后,如果线程的引用设置为 null ,那么线程会发生什么?它停止吗?是否符合GC?

After a thread started, if the reference of the thread is set to null, what will happen to the thread? Does it stop? Is it eligible for GC?

像这样:

Like this:

t.start();
t = null;


推荐答案

实时线程将继续运行,即使其参考设置为null。

就像任何其他对象一样,当没有对它的引用时,它就符合GC的条件。棘手的一点是,即使你的程序没有保留一个线程,一个正在运行的线程在ThreadGroup中也有一个引用,因此一个 运行线程永远不会被GCed。

Live thread will continue running even its reference is set to null.
Just like any other object, when there are no references to it, it is eligible to GC. The tricky point is that a running thread has a reference in a ThreadGroup even if your program does not retain one, thus a running thread is never GCed.

这篇关于Java线程设置为null时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆