1.2.4 守护线程
Java中有两种线程,一种是用户线程,另一种是守护线程。
用户线程是指用户自定义创建的线程,主线程停止,用户线程不会停止。
守护线程当进程不存在或主线程停止,守护线程也会被停止
public class Demo4Daemon {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
System.out.println("子线程..." + i);
}
}
});
//thread.setDaemon(true);
thread.start();
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(10);
System.out.println("主线程" + i);
} catch (Exception e) {
}
}
System.out.println("主线程执行完毕!");
}
}