当前位置: 首页 > news >正文

java并发编程 守护线程 用户线程 main

经常使用线程,没有对守护线程和用户线程的区别做彻底了解

下面写4个例子来验证一下

源码如下

/* Whether or not the thread is a daemon thread. */
private boolean     daemon = false;/*** Marks this thread as either a {@linkplain #isDaemon daemon} thread* or a user thread. The Java Virtual Machine exits when the only* threads running are all daemon threads.** <p> This method must be invoked before the thread is started.** @param  on*         if {@code true}, marks this thread as a daemon thread** @throws  IllegalThreadStateException*          if this thread is {@linkplain #isAlive alive}** @throws  SecurityException*          if {@link #checkAccess} determines that the current*          thread cannot modify this thread*/
public final void setDaemon(boolean on) {checkAccess();if (isAlive()) {throw new IllegalThreadStateException();}daemon = on;
}

看注释的含义是,daemon 为 true 时是守护线程,false 为用户线程。当所有守护线程在执行时 jvm 会退出。

守护线程无循环逻辑

public class DaemonThreadTest {public static void main(String[] args) {Thread thread = new Thread(() -> {System.out.println("这是一个守护线程");});// 设置守护线程thread.setDaemon(true);thread.start();}
}

执行结果无内容,说明 main 在执行完后就结束了,子线程没来得及执行。

守护线程有循环逻辑

public class DaemonThreadCycleTest {public static void main(String[] args) {Thread thread = new Thread(() -> {while (true) {}});// 设置守护线程thread.setDaemon(true);thread.start();System.out.println("主线程测试守护线程结束");}
}

执行结果

主线程测试守护线程结束

同上

用户线程无循环逻辑

public class UserThreadTest {public static void main(String[] args) {Thread thread = new Thread(() -> {System.out.println("这是一个用户线程");});thread.start();}
}

执行结果

这是一个用户线程

主线程结束后,子线程执行了。

用户线程有循环逻辑

public class UserThreadCycleTest {public static void main(String[] args) {Thread thread = new Thread(() -> {while (true) {}});thread.start();System.out.println("主线程测试用户线程结束");}
}

执行结果

主线程测试用户线程结束

此时,idea 显示程序执行还未结束,查看 jps 信息如下

C:\Users\Rike>jps
27776
852 Jps
34568
29100 UserThreadCycleTest
29468 RemoteMavenServer
30684 Launcher

说明主线程结束了,但是用户线程还在执行。

结论

守护线程的一个实现,jvm的垃圾回收,需要时创建,不需要时销毁。

如果希望 main 线程在结束后子线程马上结束,在创建时显式设置为守护线程。

如果希望 main 线程在结束后子线程继续执行,在创建时设置为用户线程或者不指定(默认为用户线程)。

决定程序是否还执行的是用户线程,但是用户线程一直持续会有资源消耗的问题。因为jvm每个线程与cpu一一对应,线程存在会一直占用操作系统和机器资源。

考虑到执行任务的问题,例如读取或者写入文件的时候,使用用户线程为好,但是需要设置退出策略。

疑问

https://www.jianshu.com/p/91028dca187b

个人猜测,main 线程是用户线程,如果是守护线程不符合逻辑,因为很多线程建立在它基础上,需要做很多关闭操作。看到这篇文章也是这样讲,但是没讲证据。正确性需要后面进行验证。

考虑到了线程池 ThreadPoolExecutor,看一下创建的线程是什么线程

ThreadPoolExecutor

public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,Executors.defaultThreadFactory(), defaultHandler);}

如果 ThreadPoolExecutor 创建时 ThreadFactory 不指定,默认是 Executors.defaultThreadFactory(),可以自己指定。

ThreadFactory  建议是自己指定,在实际项目执行时如果有多个线程池未进行指定会无法区别是哪个线程池在执行,出现问题也不好排查,指定了线程池名称,通过日志可以快速定位到问题。

Executors

static class DefaultThreadFactory implements ThreadFactory {private static final AtomicInteger poolNumber = new AtomicInteger(1);private final ThreadGroup group;private final AtomicInteger threadNumber = new AtomicInteger(1);private final String namePrefix;DefaultThreadFactory() {SecurityManager s = System.getSecurityManager();group = (s != null) ? s.getThreadGroup() :Thread.currentThread().getThreadGroup();namePrefix = "pool-" +poolNumber.getAndIncrement() +"-thread-";}public Thread newThread(Runnable r) {Thread t = new Thread(group, r,namePrefix + threadNumber.getAndIncrement(),0);if (t.isDaemon())t.setDaemon(false);if (t.getPriority() != Thread.NORM_PRIORITY)t.setPriority(Thread.NORM_PRIORITY);return t;}}

Thread

public Thread(ThreadGroup group, Runnable target, String name,long stackSize) {init(group, target, name, stackSize);}

最终执行到 init(),发现通过当前线程运行是否是守护线程来决定是否创建守护线程。

可以看到,currentThread()默认取的是当前线程,然而当前线程一般是 main 线程,这就说明了线程池创建的线程默认是用户线程,想到这里符合后台任务处理的情况,防止主程序退出了后台任务也结束了。

参考链接

https://mp.weixin.qq.com/s/j_dwm-foKDTghMxwmdsz2w

https://blog.csdn.net/MaYuKang/article/details/121931517 

https://blog.csdn.net/weixin_34268604/article/details/114863702

http://www.lryc.cn/news/180741.html

相关文章:

  • wxWidgets(1):在Ubuntu 环境中搭建wxWidgets 库环境,安装库和CodeBlocks的IDE,可以运行demo界面了,继续学习中
  • [VIM]VIM初步学习-3
  • RocketMQ Dashboard说解
  • 【RabbitMQ实战】05 RabbitMQ后台管理
  • PHP8中final关键字的应用-PHP8知识详解
  • 基于Java的校园失物招领平台设计与实现(源码+lw+部署文档+讲解等)
  • 〔024〕Stable Diffusion 之 模型训练 篇
  • 【MySQL入门到精通-黑马程序员】MySQL基础篇-DML
  • 【ARMv8 SIMD和浮点指令编程】NEON 加载指令——如何将数据从内存搬到寄存器(LDxLDxR)?
  • 华为云云耀云服务器L实例评测 | 实例场景体验之搭建个人博客:通过华为云云耀云服务器构建个人博客
  • 问题记录 springboot 事务方法中使用this调用其它方法
  • 【Spring Cloud】Ribbon 实现负载均衡的原理,策略以及饥饿加载
  • Linux下基本指令(上)
  • C++ 并发编程实战 第十一章 多线程应用的测试和除错
  • Redis实现API访问频率限制
  • BGP服务器租用价格表_腾讯云PK阿里云
  • 时序分解 | Matlab实现SSA-VMD麻雀算法优化变分模态分解时间序列信号分解
  • 【CSS如何实现双飞翼布局】
  • 服务注册发现机制
  • 【postgresql 基础入门】多表联合查询 join与union 并,交,差等集合操作,两者的区别之处
  • 很可惜,pyinstaller不是万能的
  • 0/1背包问题
  • Redis入门到精通——00数据类型
  • PADS9.5使用记录
  • Axios post请求出现500错误
  • 【Leetcode】171.Excel 表列序号
  • 2023湖南省赛游记/题解
  • 海信电视U8KL使用体验:参数卷,画质技术也独有!
  • E. Mishap in Club
  • UE4 自带体积云应用