假设我做了以下...
//MyRunnable是我声明的一个类,它实现了Runnable。
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
r = null;
像我在上面的代码片段中那样将 r 设置为 null 有什么含义?
最佳答案
让我用数字向您解释一下:
1-在
MyRunnable r = new MyRunnable();
你正在创建类 MyRunnable
的新实例,它主要实现了 Runnable
接口(interface):
2-在
Thread t = new Thread(r);
您正在创建一个新线程并通过值 传递引用 r 指向的对象:
3-在
r = null;
您正在删除 r 引用和 MyRunnable
对象之间的链接,Thread t
正在使用该对象运行线程:
关于Java : What happens if a Runnable that is being used in a thread is set to null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11011666/