请大虾帮忙 thinkin java里面的问题把SimpleThread.java中的所有线程修改成后台线程,并验证一旦main()退出,程序立刻终止。下面是 SimpleThread.java
import java.util.concurrent.*;
import static net.mindview.util.Print.*;public class SimpleDaemons implements Runnable {
public void run() {
try {
while(true) {
TimeUnit.MILLISECONDS.sleep(100);
print(Thread.currentThread() + " " + this);
}
} catch(InterruptedException e) {
print("sleep() interrupted");
}
}
public static void main(String[] args) throws Exception {
for(int i = 0; i < 10; i++) {
Thread daemon = new Thread(new SimpleDaemons());
daemon.setDaemon(true); // Must call before start()
daemon.start();
}
print("All daemons started");
TimeUnit.MILLISECONDS.sleep(175);
}
谢谢帮忙 谢谢
import java.util.concurrent.*;
import static net.mindview.util.Print.*;public class SimpleDaemons implements Runnable {
public void run() {
try {
while(true) {
TimeUnit.MILLISECONDS.sleep(100);
print(Thread.currentThread() + " " + this);
}
} catch(InterruptedException e) {
print("sleep() interrupted");
}
}
public static void main(String[] args) throws Exception {
for(int i = 0; i < 10; i++) {
Thread daemon = new Thread(new SimpleDaemons());
daemon.setDaemon(true); // Must call before start()
daemon.start();
}
print("All daemons started");
TimeUnit.MILLISECONDS.sleep(175);
}
谢谢帮忙 谢谢
关键在于这句话daemon.setDaemon(true); 你改成false试试就明白了.
main推出,别的线程也结束了.