意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Java守护线程和用户线程

来源:恒创科技 编辑:恒创科技编辑部
2024-01-10 17:04:59


线程分两种:
守护线程(Daemon)
用户线程

守护线程Thread.setDaemon(true)设置
一般程序使用用户线程,特殊的如垃圾回收线程使用守护线程


Java守护线程和用户线程

守护线程为用户线程服务的,用户线程关闭了,守护线程也会关闭。
守护线程定义要在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");
}

}


上一篇: redis应用 下一篇: 手机怎么远程登录云服务器?