日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/21 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 高并发编程-Daemon Thread的创建以及使用场景分析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 官方文檔
  • Daemon Thread VS User Thread
  • Daemon thread的特點
  • 方法
    • void setDaemon(boolean status)
    • boolean isDaemon()
  • Exceptions in Daemon thread
  • 例子
  • 使用場景分析

官方文檔

我們以JAVA8的doc為例 戳這里


Daemon Thread VS User Thread

Java提供兩種類型的線程:用戶線程和守護(hù)程序線程。

  • 用戶線程是高優(yōu)先級線程。 JVM將在終止任務(wù)之前等待任何用戶線程完成其任務(wù)。
  • 守護(hù)程序線程是低優(yōu)先級線程, 其唯一作用是為用戶線程提供服務(wù)

由于守護(hù)程序線程旨在為用戶線程提供服務(wù),并且僅在用戶線程運行時才需要,因此一旦所有用戶線程完成執(zhí)行,它們都不會阻止JVM退出。

這也就是為什么通常存在于守護(hù)程序線程中的無限循環(huán)不會導(dǎo)致問題,因為任何代碼(包括finally塊)都不會在所有用戶線程完成執(zhí)行后執(zhí)行。因此,不建議將守護(hù)程序線程用于I / O任務(wù)。

但是,這條規(guī)則有例外。守護(hù)程序線程中設(shè)計糟糕的代碼可能會阻止JVM退出。例如,在正在運行的守護(hù)程序線程上調(diào)用Thread.join()可以阻止應(yīng)用程序的關(guān)閉。


Daemon thread的特點

  • 當(dāng)所有用戶線程完成執(zhí)行時,它們無法阻止JVM退出。

  • 當(dāng)所有用戶線程完成執(zhí)行時,JVM會自行終止

  • 如果JVM發(fā)現(xiàn)正在運行的守護(hù)程序線程,它將終止該線程并在該關(guān)閉后自行終。 JVM不關(guān)心守護(hù)程序線程是否正在運行。

  • 這是一個極低優(yōu)先級的線程。


方法

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.

此方法用于將當(dāng)前線程標(biāo)記為守護(hù)程序線程或用戶線程。

舉個例子:

如果有一個用戶線程tU,那么tU.setDaemon(true)會使它成為守護(hù)程序線程

如果有一個守護(hù)程序線程tD,那么通過調(diào)用tD.setDaemon(false)會使它成為用戶線程。


boolean isDaemon()

public final boolean isDaemon() returns: This method returns true if this thread is a daemon thread; false otherwise

此方法用于檢查當(dāng)前是守護(hù)進(jìn)程。 如果線程是守護(hù)進(jìn)程,則返回true,否則返回false。


Exceptions in Daemon thread

如果在啟動線程后調(diào)用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 startedt1.setDaemon(true);t2.start();} }


例子

package com.artisan.test;import java.time.LocalDateTime;public class DaemonThread extends Thread {public DaemonThread(String name) {super(name);}@Overridepublic void run() {// Checking whether the thread is Daemon or notif (Thread.currentThread().isDaemon()) {try {System.out.println(getName() + " is Daemon thread : running " + LocalDateTime.now());// 休眠200sThread.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());// 休眠5sThread.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 Daemont1.setDaemon(true);// starting first 2 threadst1.start();t2.start();// Setting user thread t3 to Daemont3.setDaemon(true);t3.start();System.out.println(Thread.currentThread().getName() + ": over " + LocalDateTime.now());} }

執(zhí)行結(jié)果:

  • setDaemon(true) 設(shè)置為Daemon Thread
  • JVM將在終止任務(wù)之前等待任何用戶線程完成其任務(wù),JVM不關(guān)心守護(hù)程序線程是否正在運行,當(dāng)用戶線程結(jié)束后將退出。 從日志中我們可以看到t2是個user thread ,休眠了5秒,t3是daemon thread 休眠200秒,但是我們看到t2 用戶線程執(zhí)行完成后,jvm就退出了,雖然t3 daemon thread 還在進(jìn)行中,這個時候t3已經(jīng)被終止了。

  • 使用場景分析

    心跳檢測

    A ----------------------------------------------------------------------------- B
    –>Daemon Thread(Health Check)

    舉個例子: 當(dāng)A到B建立了一個長連接 ,長連接是需要發(fā)心跳的,維持這個連接。 這個時候可以在中開啟一個Daemon Thread用于心跳檢測,當(dāng)A死掉的時候,這個Daemon Thread 也會被JVM終止掉,就避免了A和B之間已經(jīng)斷開,但是心跳檢測可能報錯了但一直不退出的情況的發(fā)生。

    總結(jié)

    以上是生活随笔為你收集整理的高并发编程-Daemon Thread的创建以及使用场景分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。