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


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


ThreadGroup類setDaemon()方法

  • setDaemon() 方法在 java.lang 包中可用。
  • setDaemon() 方法用於設置該線程組的守護進程行為。正如我們所知,當該線程組的最後一個線程退出或該線程組的最後一個線程組被銷毀時,守護線程組被顯式銷毀。
  • setDaemon() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • setDaemon() 方法在分配此線程組的守護程序行為時可能會拋出異常。
    SecurityException- 當前線程不允許更新此線程組時,可能會拋出此異常。

用法:

    public final void setDaemon(boolean daemon_th);

參數:

  • boolean daemon_th- 表示該線程組的守護進程狀態,當它設置為真時,這個組被稱為守護線程組,否則它設置為假,這個組被稱為非守護線程組。

返回值:

這個方法的返回類型是void- 它什麽都不返回。

例:

// Java program to demonstrate the example 
// of void setDaemon(boolean daemon_th ) method of ThreadGroup 

public class SetDaemon implements Runnable {
    public static void main(String[] args) {
        SetDaemon set_dae = new SetDaemon();
        set_dae.setDaem();
    }

    public void setDaem() {
        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");

            // By using setDaemon() method is to set the
            // daemon behaviour for both the thread group
            // base and derived
            base.setDaemon(true);
            derived.setDaemon(false);

            // 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.....");
            th2.setPriority(10);

            // By using start() method is to start its execution 
            // of thread th2
            th2.start();

            // By using isDaemon() method is to
            // return the daemon status of the thread group
            boolean b1 = base.isDaemon();
            boolean b2 = derived.isDaemon();

            System.out.println("Is" + " " + base.getName() + " " + "daemon thread group:" + " " + b1);
            System.out.println("Is" + " " + derived.getName() + " " + "daemon thread group:" + " " + 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-1 begins.....
Thread-0 ends.....
Thread-1 ends.....
Is Base ThreadGroup daemon thread group:true
Is Derived ThreadGroup daemon thread group:false


相關用法


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