请教高手给看下这个程序,为什么按Enter程序会结束,而程序去掉setDaemon(true)后按Enter程序不会结束,这个函数什么意思啊?public class hello extends Thread{ /**
* @param args
*/
String threadName;
int delay;
public hello(String threadName, int delay)
{
this.threadName=threadName;
this.delay=delay;
setDaemon(true); //去掉后按Enter程序不会结束
}
public void run()
{
try{
while(true)
{
System.out.println(threadName);
sleep(delay);
}
}catch(Exception e){}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
hello thread1=new hello("thread1",100);
hello thread2=new hello("thread2",200);
hello thread3=new hello("thread3",300);
thread1.start();
thread2.start();
thread3.start();
System.out.println("press \"enter\" to stop.");
try{
System.in.read();
}catch(Exception e){}
System.out.println("End of main()");
}
}
* @param args
*/
String threadName;
int delay;
public hello(String threadName, int delay)
{
this.threadName=threadName;
this.delay=delay;
setDaemon(true); //去掉后按Enter程序不会结束
}
public void run()
{
try{
while(true)
{
System.out.println(threadName);
sleep(delay);
}
}catch(Exception e){}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
hello thread1=new hello("thread1",100);
hello thread2=new hello("thread2",200);
hello thread3=new hello("thread3",300);
thread1.start();
thread2.start();
thread3.start();
System.out.println("press \"enter\" to stop.");
try{
System.in.read();
}catch(Exception e){}
System.out.println("End of main()");
}
}
将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java 虚拟机退出。
该方法必须在启动线程前调用。说的很明白嘛