Java守护线程和用户线程
线程分两种:
守护线程(Daemon)
用户线程
守护线程Thread.setDaemon(true)设置
一般程序使用用户线程,特殊的如垃圾回收线程使用守护线程
守护线程为用户线程服务的,用户线程关闭了,守护线程也会关闭。
守护线程定义要在Thread.start()之前
优先级一般较低
@Test
public void testT() {
System.out.println("testT start");
Thread thread = new MyThread();
thread.setName("守护线程");
thread.setDaemon(true);
thread.start();
System.out.println("testT end");
}
class MyThread extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getId());
System.out.println("start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.error("e:", e);
}
System.out.println("end");
}
}
版权声明
本文仅代表作者观点,不代表博信信息网立场。
上一篇:详解PHP中经典排序算法的实例是怎样的 下一篇:高防服务器多少钱,价格范围