我知道线程可以是守护进程或非守护进程。我们可以使用 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
这里系统也是一个非守护进程线程组。线程组如何成为守护程序?我的意思是守护进程线程组的属性是什么?系统线程组如何是非守护程序?
与 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 子线程组中的所有线程都已死亡,除了我们标记为守护进程线程组的最后一个线程组之外,线程组仍然存在。