首頁  >  文章  >  Java  >  Java 中的守護線程

Java 中的守護線程

WBOY
WBOY原創
2024-08-30 16:03:14429瀏覽

java中的守護線程是優先權最低的線程,用於為線程提供服務,進而用於執行後台任務。在背景執行的任務包括垃圾收集和許多其他涉及 JVM 的任務,以防止其在退出後同時執行。 Java 虛擬機器在發現其他實際上不是守護線程的使用者執行緒時自行終止。如果 Java 虛擬機器發現 Daemon 線程,那麼它會先終止該線程,然後它會自己關閉。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

Daemon Thread 沒有特定的語法,但可以作為 Daemon Thread 的一部分透過其他方法建立和調用,表示為:

public class DaemonThread extends Thread
  • public 類別: 類別的存取修飾符是 public 且
  • DaemonThread:執行緒的型別是DaemonThread,因此它繼承並擴充自Thread類別。
  • Thread:執行緒類別用於將Thread類別的屬性繼承給Daemon執行緒。

此外,它還涉及各種方法,這些方法具有不同的語法來呼叫 Daemon 線程。

public final void setDaemon(boolean on)

此方法語法將參數作為布林值傳遞,用於因為當守護線程標記為布林值時,該線程是守護線程,這表示 true。

public final boolean isDaemon()

如果找到的線程是守護線程,則此方法傳回值 true;如果找到的線程為 false,則傳回 false。

Java 中的守護線程是如何運作的?

守護線程是用於建立線程並繼承其父線程的所有屬性的線程。因此,主方法中存在的所有執行緒都稱為子方法,子方法繼承了父類別的大部分執行屬性並執行任何任務。它們也稱為非守護線程。

這是透過呼叫所有將線程設定為守護線程的方法將非守護線程或使用者線程設定為守護線程的真實情況。可以使用 Thread 類別的 setDaemon() 方法輕鬆將其設定為守護線程,該類別還擴展了可運行介面。

需要記住的一個非常重要的一點是,每當JVM 啟動時,就會創建一個名為main 的線程,並且任何程式都會在此時運行或啟動,或者我們可以說這是執行流程開始的點。預設情況下,所有執行緒都將在此主執行緒上運行,直到啟動新執行緒。如果啟動新線程,子線程將負責建立任何新方法。

然後是負責建立和確定守護線程的方法,其中涉及到setDaemon方法等方法,該方法會根據傳遞給該方法的參數值來確定創建的線程是守護線程還是非守護線程。

公共布林線程 isDaemon() 另一方面用於根據返回類型檢查線程的狀態;如果為 false,則該線程是非守護線程;否則,它是守護程序。

方法

以下是下面提到的方法

1. public void setDaemon(布林狀態)

此方法用於根據傳入的參數判斷目前執行緒執行為守護線程,如果傳入的參數為boolean,則該線程為守護線程;如果傳入的參數不為boolean,則該線程為非守護線程.

它還捕獲異常,例如 IllegalThreadStateException 或 Security Exception。如果確定要捕獲非法ThreadStateException,那麼先決條件是該執行緒必須僅處於活動狀態。如果目前執行緒無法修改,則會使用Security Exception。

2. public boolean isDaemon()

該方法用於驗證當前情況或根據返回類型說出當前狀態;如果返回類型為true,那麼它是一個守護線程;否則,它是非守護線程。

Java 中守護線程的範例

以下是下面提到的範例:

範例#1

本程式示範了使用void setDaemon(boolean status) 將線程設定為守護線程以進行進一步操作的方法,以及在識別守護線程後調用一次isDaemon() 方法,該方法表示該線程集是守護線程還是守護線程非守護線程取決於參數設定的布林值。

代碼:

public class Daemon_Thread_Demo_set extends Thread {
public Daemon_Thread_Demo_set(String nm){
super(nm);
}
public void run()
{
if(Thread.currentThread().isDaemon())
{
System.out.println("Current_Thread is a Daemon thread" + Thread.currentThread().getName());
}
else
{
System.out.println("Current thread is a non-daemon thread. "+ Thread.currentThread().getName());
}
}
public static void main(String[] args) {
Daemon_Thread_Demo_set nm1 = new Daemon_Thread_Demo_set("nm1");
Daemon_Thread_Demo_set nm2 = new Daemon_Thread_Demo_set("nm2");
Daemon_Thread_Demo_set nm3 = new Daemon_Thread_Demo_set("nm3");
nm1.setDaemon(true);
nm1.start();
nm2.start();
nm3.setDaemon(false);
nm3.start();
}
}

輸出:

Java 中的守護線程

Example #2

This program demonstrates the illegal exceptions which are thrown once the current thread is identified as the Daemon thread but then the value when the set is not coming out to be set value matched with the thread it will be throwing the illegal exception as represented in the output.

Code:

public class Daemon_thrd_demo2 extends Thread {
public void run()
{
System.out.println("Current_thread_Name: "+Thread.currentThread().getName());
System.out.println("thread_as_Daemon"+Thread.currentThread().isDaemon());
}
public static void main(String[] args) {
Daemon_thrd_demo2 tst1 = new Daemon_thrd_demo2();
Daemon_thrd_demo2 tst2 = new Daemon_thrd_demo2();
tst1.start();
tst1.setDaemon(true);
tst2.start();
}
}

Output:

Java 中的守護線程

Note: The above program gives the Exception because the user thread should be set as a daemon thread only before calling the setDaemon() method. If it is not called in this way, then the thread after invocation will throw these exceptions while execution. Also, all non-daemon thread and daemon thread should be set up keeping in mind the place, i.e. before the child class or before the parent class.

Conclusion

Daemon thread and non-daemon thread have differences in them. It is very important to identify these differences as it can lead to conflicts and exception because daemon thread is a kind of thread that has the lowest priority compared to other threads while execution.

以上是Java 中的守護線程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 線程轉儲下一篇:Java 線程轉儲