守护线程 虚拟机分为用户线程和守护线程 虚拟机必须确保用户线程执行完毕 虚拟机不需要等待守护线程执行完毕
package state;
public class TestDaemon {
public static void main(String[] args) {
sys sys = new sys();
user user = new user();
Thread sysThread = new Thread(sys);
sysThread.setDaemon(true);
sysThread.start();
new Thread(user).start();
}
}
class sys implements Runnable{
@Override
public void run() {
while (true){
System.out.println("守护线程启动");
}
}
}
class user implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("用户线程" + i);
}
System.out.println("用户线程结束");
}
}
等待虚拟机结束,守护线程结束
|