ThreadGroup類isDaemon()方法
- isDaemon() 方法在 java.lang 包中可用。
- isDaemon() 方法用於檢查該線程組是否為守護線程組。
- 正如我們所知,當它的最後一個線程被銷毀或它的最後一個線程組被銷毀時,守護線程組被隱式銷毀。
- isDaemon() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- isDaemon() 方法在檢查此線程組的守護進程行為時不會拋出異常。
用法:
public final boolean isDaemon();
參數:
- 它不接受任何參數。
返回值:
這個方法的返回類型是Boolean
– 當該線程組是守護線程組時返回真,否則返回假。
例:
// Java program to demonstrate the example
// of boolean isDaemon()method of ThreadGroup
public class Daemon implements Runnable {
public static void main(String[] args) {
Daemon damn = new Daemon();
damn.isDaemn();
}
public void isDaemn() {
try {
// Create two thread group and the named are base
// and derived
ThreadGroup base = new ThreadGroup("Base ThreadGroup");
ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");
// create two threads
Thread th1 = new Thread(base, this);
Thread th2 = new Thread(derived, this);
// By using getName() method is to retrieve the
// name of the thread th1
System.out.println(th1.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th1
th1.start();
// By using getName() method is to retrieve the
// name of the thread th2
System.out.println(th2.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th2
th2.start();
System.out.println();
// By using isDaemon() method is to check the
// daemon status of the thread group
boolean b1 = base.isDaemon();
boolean b2 = derived.isDaemon();
System.out.println("Is" + " " + base.getName() + " " + "daemon threadgroup:" + " " + b1);
System.out.println("Is" + " " + derived.getName() + " " + "daemon threadgroup:" + " " + b2);
// By using join() method is to wait the current
// thread till complete execution of another
// thread
th1.join();
th2.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// Override run()
public void run() {
for (int k = 0; k < 100; ++k)
++k;
System.out.println(Thread.currentThread().getName() + " " + "ends.....");
}
}
輸出
Thread-0 begins..... Thread-0 ends..... Thread-1 begins..... Thread-1 ends..... Is Base ThreadGroup daemon threadgroup:false Is Derived ThreadGroup daemon threadgroup:false
相關用法
- Java ThreadGroup isDestroyed()用法及代碼示例
- Java ThreadGroup interrupt()用法及代碼示例
- Java ThreadGroup enumerate()用法及代碼示例
- Java ThreadGroup getMaxPriority()用法及代碼示例
- Java ThreadGroup getParent()用法及代碼示例
- Java ThreadGroup getName()用法及代碼示例
- Java ThreadGroup parentOf()用法及代碼示例
- Java ThreadGroup suspend()用法及代碼示例
- Java ThreadGroup setDaemon()用法及代碼示例
- Java ThreadGroup checkAccess()用法及代碼示例
- Java ThreadGroup resume()用法及代碼示例
- Java ThreadGroup toString()用法及代碼示例
- Java ThreadGroup destroy()用法及代碼示例
- Java ThreadGroup list()用法及代碼示例
- Java ThreadGroup stop()用法及代碼示例
- Java ThreadGroup activeCount()用法及代碼示例
- Java ThreadGroup setMaxPriority()用法及代碼示例
- Java ThreadGroup activeGroupCount()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java ThreadGroup isDaemon() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。