简体   繁体   English

什么是JAVA中的守护线程组?

[英]What is a daemon thread group in JAVA?

I know that a thread can be daemon or non-daemon.我知道一个线程可以是守护进程或非守护进程。 We can use isDaemon() method to check if thread is daemon or not.我们可以使用 isDaemon() 方法来检查线程是否是守护进程。 isDaemon() method also works on thread groups. isDaemon() 方法也适用于线程组。

class MyThread extends Thread
{
 MyThread(ThreadGroup g, String name)
 {
  super(g,name);
 }
 public void run()
 {
  long i = 0;
  for(long l=0; l<999999999; l++)
  {
   i=i+3;
  }
 }
}

class Check
{
 public static void main(String[] args)
 {
  ThreadGroup sys = Thread.currentThread().getThreadGroup().getParent();
  ThreadGroup parent = new ThreadGroup("parent");
  MyThread t1 = new MyThread(parent, "t1");
  ThreadGroup child = new ThreadGroup(parent,"child");
  Thread t2 = new Thread(child, "t2");
  t1.start();
  t2.start();
  ThreadGroup[] t = new ThreadGroup[sys.activeGroupCount()];
  sys.enumerate(t);
  for(ThreadGroup ti: t)
  {
    System.out.println(ti.getName()+"  "+ti.isDaemon());
  }
    System.out.println(sys.getName()+"  "+sys.isDaemon());
}

Output:输出:

main  false
parent  false
child  false
system  false

Here System is also a non-daemon thread group.这里 System 也是一个非守护线程组。 How a thread group can be daemon?一个线程组如何成为守护进程? I mean what are the properties of a daemon thread group?我的意思是守护线程组的属性是什么? How system thread-group is non-daemon?如何系统线程组是非守护进程?

The same way as Thread: java.lang.ThreadGroup#setDaemon . 与Thread相同的方法: java.lang.ThreadGroup#setDaemon When you create a thread group you can mark it as daemon. 创建线程组时,可以将其标记为守护程序。

As per javadoc: 根据javadoc:

A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed. 守护程序线程组在其最后一个线程停止时或在其最后一个线程组被破坏时会自动销毁。

Yes, you can set Thread group as daemon thread. 是的,您可以将“线程组”设置为守护程序线程。

/**
 * Changes the daemon status of this thread group.
 * <p>
 * First, the <code>checkAccess</code> method of this thread group is
 * called with no arguments; this may result in a security exception.
 * <p>
 * A daemon thread group is automatically destroyed when its last
 * thread is stopped or its last thread group is destroyed.
 *
 * @param      daemon   if <code>true</code>, marks this thread group as
 *                      a daemon thread group; otherwise, marks this
 *                      thread group as normal.
 * @exception  SecurityException  if the current thread cannot modify
 *               this thread group.
 * @see        java.lang.SecurityException
 * @see        java.lang.ThreadGroup#checkAccess()
 * @since      JDK1.0
 */

Just like threads, Thread groups can also be daemon and non daemon.就像线程一样,线程组也可以是守护进程和非守护进程。 Daemon thread group does not mean that it will have all daemon threads in it.守护线程组并不意味着它将包含所有守护线程。

A daemon thread group is a threadgroup that is automatically destroyed when its last thread is stopped or its last thread group is destroyed.守护线程组是在其最后一个线程停止或最后一个线程组被销毁时自动销毁的线程组。

A non daemon thread group is not destroyed automatically even if there are no active threads in it or no child thread groups.即使非守护线程组中没有活动线程或没有子线程组,也不会自动销毁它。

Consider this code :考虑这个代码:

We will be creating the following hierarchy of thread groups and each thread group will have the mentioned threads in it.我们将创建以下线程组层次结构,每个线程组中都将包含上述线程。

Main Threadgroup - 2 threads: one main , one thread-1
|
child Threadgroup - 1 thread: thread-2
|
child child Threadgroup - 1 thread: thread-3
|
child child child Threadgroup (daemon thread group) - 1 thread : thread-4

Code:代码:

class CustomRunnable implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(Thread.currentThread().getThreadGroup(), new CustomRunnable(), "Thread-1");

        ThreadGroup childOfMainThreadGroup = new ThreadGroup("childOfMainThreadGroup");
        Thread t2 = new Thread(childOfMainThreadGroup, new CustomRunnable(), "Thread-2");

        ThreadGroup childChildOfMainThreadGroup = new ThreadGroup(childOfMainThreadGroup, "childChildOfMainThreadGroup");
        Thread t3 = new Thread(childChildOfMainThreadGroup, new CustomRunnable(), "Thread-3");

        // We will create a daemon thread group
        ThreadGroup childChildChildOfMainThreadGroup = new ThreadGroup(childChildOfMainThreadGroup, "childChildChildOfMainThreadGroup");
        childChildChildOfMainThreadGroup.setDaemon(true);
        // This is non daemon thread in it
        Thread t4 = new Thread(childChildChildOfMainThreadGroup, new CustomRunnable(), "Thread-4");


        t1.start();
        t2.start();
        t3.start();
        t4.start();

        Thread.currentThread().getThreadGroup().list();
        System.out.println(Thread.currentThread().getThreadGroup().activeCount());
        System.out.println(Thread.currentThread().getThreadGroup().activeGroupCount());

        // Main thread waits for all threads to complete
        t1.join();
        t2.join();
        t3.join();
        t4.join();

        System.out.println("-----------------");

        Thread.currentThread().getThreadGroup().list();
        System.out.println(Thread.currentThread().getThreadGroup().activeCount());
        System.out.println(Thread.currentThread().getThreadGroup().activeGroupCount());

    }
    
}

You will see that even though all threads in child thread groups of Main have died, still thread groups exist except the last one which we marked as daemon thread group.您会看到,即使 Main 的子线程组中的所有线程都已死亡,但除了最后一个我们标记为守护线程组的线程组之外,线程组仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM