if the reference of the thread is set to null.
来源:互联网 发布:印刷自动报价软件 编辑:程序博客网 时间:2024/05/25 16:37
Q. 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?
A: No.
Live thread will continue running even its reference is set to null. See JLS12.6.1
See an interesting example, run it, to see the results:
// TestThread.java
public class TestThread {
public static void main(String[] s) {
// anonymous class extends Thread
Thread t = new Thread() {
public void run() {
// infinite loop
while (true) {
try {
Thread.sleep(1000);
}catch (InterruptedException e) {
}
// as long as this line printed out, you know it is alive.
System.out.println("thread is running...");
}
}
};
t.start();
t = null;
// no more references for Thread t
// another infinite loop
while (true) {
try {
Thread.sleep(3000);
}catch (InterruptedException e) {
}
System.gc();
System.out.println("Executed System.gc()");
}
// The program will run forever until you use ^C to stop it
}
}
A: No.
Live thread will continue running even its reference is set to null. See JLS12.6.1
See an interesting example, run it, to see the results:
// TestThread.java
public class TestThread {
public static void main(String[] s) {
// anonymous class extends Thread
Thread t = new Thread() {
public void run() {
// infinite loop
while (true) {
try {
Thread.sleep(1000);
}catch (InterruptedException e) {
}
// as long as this line printed out, you know it is alive.
System.out.println("thread is running...");
}
}
};
t.start();
t = null;
// no more references for Thread t
// another infinite loop
while (true) {
try {
Thread.sleep(3000);
}catch (InterruptedException e) {
}
System.gc();
System.out.println("Executed System.gc()");
}
// The program will run forever until you use ^C to stop it
}
}
- if the reference of the thread is set to null.
- How to Tell if the I/O of the Database is Slow - 1
- How to Tell if the I/O of the Database is Slow - 2
- If t is the subString of s
- Pay Attention to the Reference of Java
- The HttpsGetEnabled property of ServiceMetdataBehavior is set to true and the HttpsGetUrl property
- Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "te
- The stack 'CSTACK' is filled to 92% (368 bytes used out of 400). The warning threshold is set to 90.%
- mysql:Variable 'character_set_client' can't be set to the value of 'NULL'解决
- Variable ‘time_zone’ can’t be set to the value of ‘NULL’解决方法
- [mysql] 批量导入提示: Variable ‘character_set_client’ can’t be set to the value of ‘NULL
- string rotation (if string is the rotate of the other)
- The value of the project attribute is null
- RYU 启动出现unsupported version 0x1. If possible, set the switch to use one of the versions[4]
- RYU 启动出现unsupported version 0x1. If possible, set the switch to use one of the versions[4]
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to th
- Unable to get value of the property '2': object is null or undefined
- Reference to Null object in the .NET Remoting Deployment?
- JDBC完全总结
- 本人新软件发布:按键伴侣2007
- 关于NOVELL 411 安装,支持服务
- 同步synchronized方法和代码块
- Process Monitor v1.1 By Mark Russinovich and Bryce Cogswell
- if the reference of the thread is set to null.
- SQL 分頁
- 转载:动态语言崛起 Delphi For PHP能否挽救Borland?
- MSXML类封装
- B-树(B-Tree)的C++演示代码
- 抵御内核恶意软件
- Mozilla研究—XPCOM语言无关性的实现原理
- 路由器,交换机,集线器的区别
- 最近时间紧张,正在开发iSpace Desktop 2007....