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

查看:36
本文介绍了当 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?

像这样:

t.start();
t = null;

推荐答案

实时线程将继续运行,即使它的引用设置为空.
就像任何其他对象一样,当没有对它的引用时,它就有资格进行 GC.棘手的一点是,正在运行的线程在 ThreadGroup 中有一个引用,即使您的程序没有保留一个引用,因此 正在运行的线程永远不会被 GC.

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天全站免登陆