高并发编程-Daemon Thread的创建以及使用场景分析
admin
2023-07-03 21:04:18
0

  官方文档

  我们以JAVA8的doc为例

  Daemon Thread VS User Thread

  Java提供两种类型的线程:用户线程和守护程序线程。

  用户线程是高优先级线程。 JVM将在终止任务之前等待任何用户线程完成其任务。

  守护程序线程是低优先级线程, 其唯一作用是为用户线程提供服务。

  由于守护程序线程旨在为用户线程提供服务,并且仅在用户线程运行时才需要,因此一旦所有用户线程完成执行,它们都不会阻止JVM退出。

  这也就是为什么通常存在于守护程序线程中的无限循环不会导致问题,因为任何代码(包括finally块)都不会在所有用户线程完成执行后执行。因此,不建议将守护程序线程用于I / O任务。

  但是,这条规则有例外。守护程序线程中设计糟糕的代码可能会阻止JVM退出。例如,在正在运行的守护程序线程上调用Thread.join()可以阻止应用程序的关闭。

  Daemon thread的特点

  当所有用户线程完成执行时,它们无法阻止JVM退出。

  当所有用户线程完成执行时,JVM会自行终止

  如果JVM发现正在运行的守护程序线程,它将终止该线程并在该关闭后自行终。 JVM不关心守护程序线程是否正在运行。

  这是一个极低优先级的线程。

  方法

  void setDaemon(boolean status)

  public final void setDaemon(boolean on)

  parameters:

  on : if true, marks this thread as a daemon thread.

  exceptions:

  IllegalThreadStateException: if only this thread is active.

  SecurityException: if the current thread cannot modify this thread.

  此方法用于将当前线程标记为守护程序线程或用户线程。

  举个例子:

  如果有一个用户线程tU,那么tU.setDaemon(true)会使它成为守护程序线程

  如果有一个守护程序线程tD,那么通过调用tD.setDaemon(false)会使它成为用户线程。

  boolean isDaemon()

  public final boolean isDaemon()

  returns:

  This method returns true if this thread is a daemon thread;

  false otherwise

  此方法用于检查当前是守护进程。 如果线程是守护进程,则返回true,否则返回false。

  Exceptions in Daemon thread

  如果在启动线程后调用setDaemon()方法,则会抛出IllegalThreadStateException。

  package com.artisan.test;

  public class DaemonThread extends Thread {

  public void run()

  {

  System.out.println("Thread name: " + Thread.currentThread().getName());

  System.out.println("Check if its DaemonThread: "

  + Thread.currentThread().isDaemon());

  }

  public static void main(String[] args)

  {

  DaemonThread t1 = new DaemonThread();

  DaemonThread t2 = new DaemonThread();

  t1.start();

  // Exception as the thread is already started

  t1.setDaemon(true);

  t2.start();

  }

  }

  

高并发编程-Daemon Thread的创建以及使用场景分析


  例子无锡妇科医院 http://www.xasgfk.cn/

  package com.artisan.test;

  import java.time.LocalDateTime;

  public class DaemonThread extends Thread {

  public DaemonThread(String name) {

  super(name);

  }

  @Override

  public void run() {

  // Checking whether the thread is Daemon or not

  if (Thread.currentThread().isDaemon()) {

  try {

  System.out.println(getName() + " is Daemon thread : running " + LocalDateTime.now());

  // 休眠200s

  Thread.sleep(200_000);

  System.out.println(getName() + " is Daemon thread: over " + LocalDateTime.now());

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  } else {

  try {

  System.out.println(getName() + " is User thread : running " + LocalDateTime.now());

  // 休眠5s

  Thread.sleep(5_000);

  System.out.println(getName() + " is User thread : over " + LocalDateTime.now());

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  }

  }

  public static void main(String[] args) {

  System.out.println(Thread.currentThread().getName() + ": running " + LocalDateTime.now());

  DaemonThread t1 = new DaemonThread("t1");

  DaemonThread t2 = new DaemonThread("t2");

  DaemonThread t3 = new DaemonThread("t3");

  // Setting user thread t1 to Daemon

  t1.setDaemon(true);

  // starting first 2 threads

  t1.start();

  t2.start();

  // Setting user thread t3 to Daemon

  t3.setDaemon(true);

  t3.start();

  System.out.println(Thread.currentThread().getName() + ": over " + LocalDateTime.now());

  }

  }

  执行结果

  setDaemon(true) 设置为Daemon Thread

  JVM将在终止任务之前等待任何用户线程完成其任务,JVM不关心守护程序线程是否正在运行,当用户线程结束后将退出。 从日志中我们可以看到t2是个user thread ,休眠了5秒,t3是daemon thread 休眠200秒,但是我们看到t2 用户线程执行完成后,jvm就退出了,虽然t3 daemon thread 还在进行中,这个时候t3已经被终止了。

  使用场景分析

  心跳检测

  A ----------------------------------------------------------------------------- B

  –>Daemon Thread(Health Check)

  举个例子: 当A到B建立了一个长连接 ,长连接是需要发心跳的,维持这个连接。 这个时候可以在中开启一个Daemon Thread用于心跳检测,当A死掉的时候,这个Daemon Thread 也会被JVM终止掉,就避免了A和B之间已经断开,但是心跳检测可能报错了但一直不退出的情况的发生。


上一篇:yum install php-* 报错

下一篇:php ob函数

相关内容

热门资讯

花费半生积蓄,农村自建房热背后 今年以来,在安徽安庆望江县做外墙漆生意的徐仙琴越来越忙碌了,她和员工们每天奔波在县城的各个村庄里,找...
黎以冲突再升级对中东地区影响几... 从当前的局势来看,黎以双方博弈已陷入谈判停滞、战火升级的恶性循环,地区冲突风险持续走高。根据以军发布...
易事达取得载带冷却定型装置专利... 国家知识产权局信息显示,浙江易事达电子材料有限公司取得一项名为“一种载带冷却定型装置”的专利,授权公...
深度推荐:2026年五大精选手... 本文全面梳理2026年手机电池批发市场主流品牌,聚焦续航升级与库存优化两大核心需求。通过对五大头部品...
法国外长要求安理会就以色列在黎... △法国外长巴罗(资料图)当地时间5月31日,法国外长巴罗宣布,他已要求召开一次联合国安全理事会紧急会...
大数据赋能矿山安全 科技先锋刘... 在煤炭产业高质量发展与智慧矿山建设加速推进的背景下,矿山安全监管正从传统人工巡查向数字化、智能化、预...
解压玩具“娜塔莎”引争议,它的... 最近,一款名为“娜塔莎”的婴儿造型“捏捏乐”解压玩具在网络上和校园里悄然流行,商家宣称其可以用来缓解...
坚持“四个面向” 矢志科技报国... 5月30日,第十个全国科技工作者日如期而至。日前,中央宣传部、中国科协向全社会发布“最美科技工作者”...
显微镜的“能源革命” ——无液... 我国自主研制闭循环光耦合SPM系统,为量子科技前沿研究提供可持续的“中国方案”。 导读 Part ...
五部门开展“百场万企”大中小企... 记者从工业和信息化部获悉,工业和信息化部、国家发展改革委、国务院国资委等五部门联合印发通知,组织开展...