繁体   English   中英

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

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

我知道一个线程可以是守护进程或非守护进程。 我们可以使用 isDaemon() 方法来检查线程是否是守护进程。 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());
}

输出:

main  false
parent  false
child  false
system  false

这里 System 也是一个非守护线程组。 一个线程组如何成为守护进程? 我的意思是守护线程组的属性是什么? 如何系统线程组是非守护进程?

与Thread相同的方法: java.lang.ThreadGroup#setDaemon 创建线程组时,可以将其标记为守护程序。

根据javadoc:

守护程序线程组在其最后一个线程停止时或在其最后一个线程组被破坏时会自动销毁。

是的,您可以将“线程组”设置为守护程序线程。

/**
 * 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
 */

就像线程一样,线程组也可以是守护进程和非守护进程。 守护线程组并不意味着它将包含所有守护线程。

守护线程组是在其最后一个线程停止或最后一个线程组被销毁时自动销毁的线程组。

即使非守护线程组中没有活动线程或没有子线程组,也不会自动销毁它。

考虑这个代码:

我们将创建以下线程组层次结构,每个线程组中都将包含上述线程。

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

代码:

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());

    }
    
}

您会看到,即使 Main 的子线程组中的所有线程都已死亡,但除了最后一个我们标记为守护线程组的线程组之外,线程组仍然存在。

暂无
暂无

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

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