ThreadGroup的作用是对加入该组的线程进行统一的管理。《Effective Java》中认为由于ThreadGroup的很多方法后期都没有维护,所以不推荐使用,建议使用线程池来代替。
主要方法如下:
构造方法
public ThreadGroup(String name)
创建一个名为name的线程组public ThreadGroup(ThreadGroup parent,String name)
创建一个名为name的线程组,并指定它的父线程组为parent
普通方法
public int activeCount();
获得当前线程组中活跃的(还在执行的)线程数目public int activeGroupCount()
获得当前线程组中活动的子线程组的数目public void checkAccess() throws SecurityException
判断当前线程是否有权限去修改该线程组。如果当前线程不允许访问该线程组就会抛出SecurityException。public int enumerate(Thread list[])
枚举当前线程组中的线程,并将它们赋予list的每一个元素,返回线程的实际个数public int enumerate(ThreadGroup list[])
枚举当前线程组中的线程组,并将它们赋予list的每一个元素,返回线程组的实际个数public final int getMaxPriority()
获得当前线程组内优先级最高的线程的优先级public final String getName()
获得当前线程组的名字public final ThreadGroup getParent()
获得当前线程组的父线程组public boolean parentOf(ThreadGroup g)
判断当前线程组是否为指定线程的父线程public boolean isDaemon()
判断该线程组是否是一个守护线程组。当最后一个线程停止或最后一个线程组销毁时,该守护线程组会自动销毁。public void list()
列出线程组及线程组中的各个线程的信息。/* 等价于 System.out.println(threadGroup.toString()); System.out.println("\t"+thread.toString()); …… */ java.lang.ThreadGroup"线程组名", maxpri="组内线程的最大优先级"] Thread[线程名, 线程优先级, 直接隶属的线程组名] ……
public final void resume()
使当前组内被挂起的线程恢复到可运行状态public final void setDaemon (boolean daemon)
设置该线程组是否是守护线程组。如果是守护线程组的话,当最后一个线程停止或最后一个线程组销毁时,该守护线程组会自动销毁。(注意:线程组设置为守护线程并不会改变线程组内的线程是否是守护线程)public final void stop()
终止当前线程组中所有线程public final void suspend()
挂起当前线程组中所有线程public String toStrinng()
将当前线程组转换为String对象,输出格式为java.lang.ThreadGroup[name="线程组名", maxpri="组内线程的最大优先级"]
代码实战
package com.demo.test;
public class ThreadGroupDemo {
private static ThreadGroup threadGroup;
public static void main(String[] args) {
threadGroup = new ThreadGroup("threadGroup");
threadGroup.setDaemon(true);
int len = 10;
for (int i = 0; i < len; i++) {
final int index = i;
Thread thread = new Thread(threadGroup, new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100 * index);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("activeCount = " + threadGroup.activeCount());
System.out.println("activeGroupCount = " + threadGroup.activeGroupCount());
Thread[] threads = new Thread[threadGroup.activeCount()];
boolean recurse = false;
threadGroup.enumerate(threads, recurse);
System.out.println("enumerate(threads, recurse) = ");
for (int j = 0; j < threads.length; j++) {
System.out.println("\tthreads[" + j + "]=" + threads[j].toString() + ", isDaemon=" + threads[j].isDaemon());
}
System.out.println("getMaxPriority = " + threadGroup.getMaxPriority());
System.out.println("isDaemon = " + threadGroup.isDaemon());
System.out.println("list = ");
threadGroup.list();
System.out.println("toString = " + threadGroup.toString());
System.out.println("checkAccess");
try {
threadGroup.checkAccess();
} catch (SecurityException e) {
e.printStackTrace();
}
System.out.println("----------------------");
}
}, "thread_" + i);
/*
* public final static int MIN_PRIORITY = 1;
* public final static int NORM_PRIORITY = 5;
* public final static int MAX_PRIORITY = 10;
*/
int priority = i;
if (priority < Thread.MIN_PRIORITY) {
priority = Thread.MIN_PRIORITY;
} else if (priority > Thread.MAX_PRIORITY) {
priority = Thread.MAX_PRIORITY;
}
thread.setPriority(priority);
thread.start();
}
}
}