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

山东兴润建设集团网站宜州设计公司

山东兴润建设集团网站,宜州设计公司,查询网站建立时间,娱乐类网页经常使用线程,没有对守护线程和用户线程的区别做彻底了解 下面写4个例子来验证一下 源码如下 /* Whether or not the thread is a daemon thread. */ private boolean daemon false;/*** Marks this thread as either a {linkplain #isDaemon daemon} thread*…

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

下面写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.yayakq.cn/news/49170/

相关文章:

  • 哪个网站可以做免费宣传mip wordpress 评论
  • wordpress站点全屏wordpress+获得子类id
  • 研磨 东莞网站建设永兴县网站建设哪个好
  • 做教育行业营销类型的网站贵州专业建网站
  • 新网站做seo优化步骤怎么往网站里做游戏
  • 为什么做的网站别的浏览器打不开怎么回事惠州市建设厅网站
  • 拖拽做网站长春市网站推广
  • 网站一年费用国外网站做网上生意哪个好
  • 怎么看网站做没做备案网站建设公
  • 公司网站建设任务书企业网站seo优化外包
  • 网站建设 工作职责wordpress 主题破解版
  • 杭州做网站的公司有哪些重庆哪里好玩
  • 建立自己公司网站的方法wordpress 模板 分类
  • 对网站建设这门课程的想法腾达建设哪里的
  • 营销型网站要素发软文的网站
  • 西北网站建设网络营销外包公司的评价
  • 怎样查询网站是否备案找培训机构的平台
  • 网站改版怎么做深圳网络整合营销公司
  • 网站建设项目的预表django怎么做网站
  • 服装设计师常用网站做推广的网站有哪些
  • 佛山建网站公司可以用电脑做网站主机吗
  • 免费建立英文网站济宁网站建设seo
  • 网站wordpress是什么意思大型服务器多少钱一台
  • 兰州市城乡和住房建设局网站百度的链接
  • 静态网站的建设友情贴吧
  • 天马网络网站女性门户网站源码
  • 游戏排行榜页游网站建设seo 视频
  • 建设门户网站的公司网站白名单 是什么
  • 微网站 开发网站建设用什么教材
  • 深圳网站建设优化推广公司凡科建站免费