import java.io.*;class Daemon extends Thread {
private Thread[] t = new Thread[10];
public Daemon() {
setDaemon(true);
start();
}
public void run() {
for(int i = 0; i < t.length; i++)
t[i] = new DaemonSpawn(i);
for(int i = 0; i < t.length; i++)
System.out.println("t[" + i + "].isDaemon() = "
+ t[i].isDaemon());
while(true)
yield();
}
}class DaemonSpawn extends Thread {
public DaemonSpawn(int i) {
start();
System.out.println("DaemonSpawn " + i + " started");
}
public void run() {
while(true)
yield();
}
}public class Daemons {
public static void main(String[] args) throws Exception {
Thread d = new Daemon();
System.out.println("d.isDaemon() = " + d.isDaemon());
// Allow the daemon threads to
// finish their startup processes:
Thread.sleep(1000);
}
}-------------------------
上面这个程序为什么最先输出的是
d.isDaemon() = true
呢
private Thread[] t = new Thread[10];
public Daemon() {
setDaemon(true);
start();
}
public void run() {
for(int i = 0; i < t.length; i++)
t[i] = new DaemonSpawn(i);
for(int i = 0; i < t.length; i++)
System.out.println("t[" + i + "].isDaemon() = "
+ t[i].isDaemon());
while(true)
yield();
}
}class DaemonSpawn extends Thread {
public DaemonSpawn(int i) {
start();
System.out.println("DaemonSpawn " + i + " started");
}
public void run() {
while(true)
yield();
}
}public class Daemons {
public static void main(String[] args) throws Exception {
Thread d = new Daemon();
System.out.println("d.isDaemon() = " + d.isDaemon());
// Allow the daemon threads to
// finish their startup processes:
Thread.sleep(1000);
}
}-------------------------
上面这个程序为什么最先输出的是
d.isDaemon() = true
呢
d.isDaemon() = true
DaemonSpawn 0 started
DaemonSpawn 1 started
DaemonSpawn 2 started
DaemonSpawn 3 started
................为什么System.out.println("d.isDaemon() = " + d.isDaemon()); 会比Thread d = new Daemon();
先得到执行啊?
大哥,去掉当然不输出拉
问题是为什么感觉System.out.println("d.isDaemon() = " + d.isDaemon()); 会比Thread d = new Daemon();
先执行
我改了下你的代码,你看看 就知道了。
import java.io.*;class Daemons extends Thread {
public static void main(String[] args) throws Exception {
Thread d = new Daemons();
System.out.println("d.isDaemon() = " + d.isDaemon());
// Allow the daemon threads to
// finish their startup processes:
// Thread.sleep(1000);
}
private Thread[] t = new Thread[10]; public Daemons() {
setDaemon(true);
start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void run() {
for (int i = 0; i < t.length; i++)
t[i] = new DaemonSpawn(i);
for (int i = 0; i < t.length; i++)
System.out.println("t[" + i + "].isDaemon() = " + t[i].isDaemon());
while (true)
yield();
}
}class DaemonSpawn extends Thread {
public DaemonSpawn(int i) {
start();
System.out.println("DaemonSpawn " + i + " started");
} public void run() {
while (true)
yield();
}
}
private int i;
public void next() { i++; i++; }
public int getValue() { return i; }
public static void main(String[] args) {
final AlwaysEven ae = new AlwaysEven();
new Thread("Watcher") {
public void run() {
while(true) {
int val = ae.getValue();
if(val % 2 != 0) {
System.out.println(val); //为什么会进入到这里呢,难道线程是在next函数执行第一个i++的时候启动了?
System.exit(0);
}
}
}
}.start();
while(true)
ae.next();
}
} ///:~
差不多