线程分为用户线程和守护线程;
虚拟机必须确保用户线程执行完毕;
虚拟机不用等待守护线程执行完毕;
如后台记录操作日志,监控内存使用等;
t.setDaemo(true); //设置用户线程为守护线程。默认的为false;
package com.wxh.statictest;
public class DaemoTest {
public static void main(String[] args) {
You1 y = new You1();
God g = new God();
Thread t1 = new Thread(y);
Thread t2 = new Thread(g);
t1.start();
t2.setDaemon(true); //设置为守护线程,false为默认用户线程
t2.start();
}
}
class You1 implements Runnable{
public void run() {
for(int i = 0;i<=365 *100;i++) {
System.out.println(i);
System.out.println("happy life....");
System.out.println("mmmmmmmmmmmm..........");
}
}
}
class God implements Runnable{
public void run() {
for(;true;) {
System.out.println("the god is blees you");
}
}
}