java - java中线程初始化为null

标签 java multithreading

public class ThreadState {

    public static void main(String[] args){
            Thread t = new Thread(){
            public void run(){
                       // infinite loop
                       while (true) {
                        try {

                         Thread.sleep(1000);
                        }
                        catch (InterruptedException e) {
                        }

                        System.out.println("thread is running..."+Thread.currentThread().toString());
                       }

            }
        };
        t.start() ; 
        t = null ;
        while (true) {
              try {

              Thread.sleep(3000);
              }
              catch (InterruptedException e) {
              }
              System.out.println("thread is running..."+Thread.currentThread().toString());
        }   

    }
}

线程实例 t 被初始化为 null ..它仍然能够运行并在控制台上打印其详细信息。需要对此进行解释

最佳答案

Thread instance t is initialized to null

不,线程 变量 设置为空值。变量不是实例 - 值得绝对确保您理解这一点。

更改变量的值根本不会影响现有的 Thread 对象。

关于java - java中线程初始化为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10655265/

相关文章:

java - Spring Boot 应用程序在远程 Tomcat 中返回 404

java - 如何从线程调用不同于 run() 的方法

c - 简单的 C pthread 测试程序在执行期间挂起

java - SQLite - WHERE 子句中 IS 和 =(等于)的区别。 (使用 JDBC PreparedStatement)

java swing 和 sqlite

c# - 如何使用 Task 设置最大并发线程数

multithreading - 如何使并发与写入 hive 表的数据帧一起工作?

multithreading - 在流程外环境中,公寓 “live”是位于服务器端还是位于客户端?

java - 我可以将任何版本的 Java SE 与 Java EE 8 一起使用吗?

java - 为什么我无法停止 Android 中的服务?