我在 Android 中像这样在 Java 的多线程中测试 Think:
private void testDeamon(){
Thread d = new Daemon();
System.out.println(
"d.isDaemon() = " + d.isDaemon());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class Daemon extends Thread {
private static final int SIZE = 10;
private static final String TAG = null;
private int i = 9000;
private Thread[] t = new Thread[SIZE];
public Daemon() {
setDaemon(true);
start();
}
public void run() {
for(int i = 0; i < SIZE; i++)
t[i] = new DaemonSpawn(i);
for(int i = 0; i < SIZE; i++)
System.out.println(
"t[" + i + "].isDaemon() = "
+ t[i].isDaemon());
while(true) {
Log.d(TAG, "Deamon running..."+ i--);
if (i==0)
break;
yield();
}
}
class DaemonSpawn extends Thread {
public DaemonSpawn(int i) {
System.out.println(
"DaemonSpawn " + i + " started");
start();
}
public void run() {
while(true)
yield();
}
}
}
为什么 spawn 守护进程的结果是错误的,所以守护进程的 spawn 不是这里的守护进程
为什么Android应用程序退出后守护程序tad一直在运行?根据 TIJ,JVM 在所有非守护线程被杀死后退出,并且守护线程也退出。那么JVM在应用程序销毁后没有关闭?谢谢。