當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java ThreadGroup isDaemon()用法及代碼示例

ThreadGroup 類的 isDaemon() 方法測試該線程組是否為守護線程組。

用法

public final boolean isDaemon()

返回

如果此線程組是守護線程組,則此方法返回 true。否則,它將返回false。

示例

class NewThread extends Thread 
{
    NewThread(String threadname, ThreadGroup tg)
    {
        super(tg, threadname);
    }
    public void run()
    {
        for(int i = 0;i < 10;i++) 
        {
            i++;
        }
        System.out.println(Thread.currentThread().getName() + " finished executing");
    }
}
public class ThreadGroupIsDaemonExp 
{
    public static void main(String arg[]) throws InterruptedException,
        SecurityException, Exception
    {
        // creating a threadGroup
        ThreadGroup tg = new ThreadGroup("Parent thread");
        // creating a thread 
        NewThread t1 = new NewThread("Thread-1", tg);
        t1.start();
        
        // returns false if this thread group is not a daemon thread group
        System.out.println("Is " + tg.getName() + " a daemon threadGroup? " + tg.isDaemon());
    }
}

輸出:

Is Parent thread a daemon threadGroup? false
Thread-1 finished executing






相關用法


注:本文由純淨天空篩選整理自 Java ThreadGroup isDaemon() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。