What will happen when a Java thread is set to null?(当 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 时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当 Java 线程设置为 null 时会发生什么?
基础教程推荐
- 将 Windows 证书导入 Java 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- JPA惰性列表上的流 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
