public class TestThreadDaemonMethod {
/**
* @param args
*/
public static void main(String[] args) {
TestThread t = new TestThread("test thread");
//如果设置为false,那么在执行完后面的start方法后,jvm不会退出
t.setDaemon(false);
//如果设置为true,则执行完后面的start方法后,jvm会退出
t.setDaemon(true);
t.start();
//在start之后设置daemon会抛出异常
//t.setDaemon(true);
if (t.isDaemon()) {
System.out.println("test thread is a daemon.");
}
}
}
class TestThread extends Thread {
private String name;
public TestThread(String name) {
this.name = name;
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
while(true) {
System.out.println("running " + name);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
posted on 2010-03-31 15:26 pony 阅读(1757) 评论(0) 编辑 收藏 所属分类: Java