日志文章

2019-12-26 aflfte2011

守护线程

package com.aflfte.state;
/**
 * 守护线程
 * 守护线程是为用户线程服务的,JVM停止不用等待守护线程执行完毕
 * 线程默认都是用户线程,虚拟机需要等待所有线程执行完毕再停止
 * 
 * @author jinhao
 *
 */
public class DaemonTest {
public static void main(String[] args) {
God god=new God();
You you=new You();

Thread t=new Thread(god);
t.setDaemon(true);//将用户线程调整为守护线程
t.start();
new Thread(you).start();
}
}
class You implements Runnable{
@Override
public void run() {
for(int i=0;i<=365*100;i++) {
System.out.println("Happy life.......");
}
System.out.println("Game Over.......");
}
}
class God implements Runnable{
@Override
public void run() {
for(;true;) {
System.out.println("bless you.......");
}
}
}


« 线程的其它方法 | 线程的优先级»