所以在下面:
public class JavaClass {
public static void main(String[] args) {
JavaClass clazz = new JavaClass();
clazz.startCustomThread();
clazz = new JavaClass();
clazz.startCustomThread();
}
public void startCustomThread() {
new MyThread().startThread();
}
private static class MyThread {
public void startThread() {
new Thread(() -> {
System.out.println("In thread " + Thread.currentThread().getName());
while (true) {
try {
Thread.sleep(1000 * 5);
System.out.println("Woke up " + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
输出是:
In thread Thread-1
In thread Thread-0
Woke up Thread-0
Woke up Thread-1
....
由于clazz应该在第二个实例之后进行 GC,并且第一个线程在第一次调用的本地范围内启动,所以startCustomThread()
我的问题是为什么第一个线程没有终止?
GCT1015
DIEA
慕村225694
相关分类