多线程_守护线程_daemon
程序员文章站
2022-05-05 21:38:08
...
package com.sxt.state;
/**
* 守护线程:是为用户线程服务的;JVM停止不用等待守护线程执行完毕
* 默认:用户线程 JVM需要等待用户线程执行完毕才会停止
* @author
*
*/
public class DaemonTest {
public static void main(String[] args) {
Thread t1 = new Thread(new God());
Thread t2 = new Thread(new You());
t1.setDaemon(true);//将用户线程调整为守护线程
t1.start();
t2.start();
}
}
class You implements Runnable{
@Override
public void run() {
for(int i=1;i<365*100;i++) {
System.out.println("happy life...");
}
System.out.println("oooooooo");
}
}
class God implements Runnable{
@Override
public void run() {
while(true) {
System.out.println("bless you...");
}
}
}