Java线程是否会被垃圾回收?
(2013-07-30 20:33:07)
标签:
threadgcweakreference线程垃圾回收 |
分类: Tech |
如果将线程启动后,然后线程变量置空,线程会怎么样?
importjava.lang.ref.WeakReference;
publicclass TestThread {
public static void testUnreferencedThread() {
// 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...");
}