当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。