所有线程都隶属于一个线程组。那可以是一个默认线程组,亦可是一个创建线程时明确指定的组。在创建之初,线程被限制到一个组里,而且不能改变到一个不同的组。每个应用都至少有一个线程从属于系统线程组。若创建多个线程而不指定一个组,它们就会自动归属于系统线程组。
线程组也必须从属于其他线程组。必须在构建器里指定新线程组从属于哪个线程组。若在创建一个线程组的时候没有指定它的归属,则同样会自动成为系统线程组的一名属下。因此,一个应用程序中的所有线程组最终都会将系统线程组作为自己的“父”
之所以要提出“线程组”的概念,一般认为,是由于“安全”或者“保密”方面的理由。根据Arnold和Gosling的说法:“线程组中的线程可以修改组内的其他线程,包括那些位于分层结构最深处的。一个线程不能修改位于自己所在组或者下属组之外的任何线程”。
运行testThreadGroup1:
运行testThreadGroup2:
线程组也必须从属于其他线程组。必须在构建器里指定新线程组从属于哪个线程组。若在创建一个线程组的时候没有指定它的归属,则同样会自动成为系统线程组的一名属下。因此,一个应用程序中的所有线程组最终都会将系统线程组作为自己的“父”
之所以要提出“线程组”的概念,一般认为,是由于“安全”或者“保密”方面的理由。根据Arnold和Gosling的说法:“线程组中的线程可以修改组内的其他线程,包括那些位于分层结构最深处的。一个线程不能修改位于自己所在组或者下属组之外的任何线程”。
测试代码:
package com.lang;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import junit.framework.TestCase;
public class TestThreadGroup extends TestCase {
public void testThreadGroup1() {
final CountDownLatch cdl = new CountDownLatch(15);
List<ThreadGroup> list = new ArrayList<ThreadGroup>();
for (int i = 0; i < 5; i++) {
list.add(new ThreadGroup("ThreadGroup" + i));
}
for (int i = 0; i < 15; i++) {
int index = i % 5;
ThreadGroup threadGroup = list.get(index);
Thread thread = new Thread(threadGroup, new Runnable() {
@Override
public void run() {
for (int j = 0; j < 3; j++) {
System.out.println(Thread.currentThread() + ", " + j);
}
cdl.countDown();
}
});
int pri = index + 1;
thread.start();
threadGroup.setMaxPriority(pri);
}
try {
cdl.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void testThreadGroup2() {
final CountDownLatch cdl = new CountDownLatch(3);
ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");
for (int i = 0; i < 3; i++) {
Thread thread = new Thread(threadGroup, new Runnable() {
@Override
public void run() {
for (int j = 0; j < 3; j++) {
System.out.println(Thread.currentThread() + ", " + j);
}
cdl.countDown();
}
});
thread.start();
}
Thread[] threads = new Thread[threadGroup.activeCount()];
threadGroup.enumerate(threads);
for (Thread thread : threads) {
if (thread != null) {
System.out.println(thread);
}
}
try {
cdl.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行testThreadGroup1:
Thread[Thread-0,5,ThreadGroup0], 0
Thread[Thread-0,5,ThreadGroup0], 1
Thread[Thread-0,5,ThreadGroup0], 2
Thread[Thread-1,5,ThreadGroup1], 0
Thread[Thread-1,5,ThreadGroup1], 1
Thread[Thread-1,5,ThreadGroup1], 2
Thread[Thread-3,5,ThreadGroup3], 0
Thread[Thread-3,5,ThreadGroup3], 1
Thread[Thread-3,5,ThreadGroup3], 2
Thread[Thread-2,5,ThreadGroup2], 0
Thread[Thread-2,5,ThreadGroup2], 1
Thread[Thread-2,5,ThreadGroup2], 2
Thread[Thread-4,5,ThreadGroup4], 0
Thread[Thread-4,5,ThreadGroup4], 1
Thread[Thread-4,5,ThreadGroup4], 2
Thread[Thread-9,5,ThreadGroup4], 0
Thread[Thread-9,5,ThreadGroup4], 1
Thread[Thread-7,3,ThreadGroup2], 0
Thread[Thread-9,5,ThreadGroup4], 2
Thread[Thread-7,3,ThreadGroup2], 1
Thread[Thread-7,3,ThreadGroup2], 2
Thread[Thread-8,4,ThreadGroup3], 0
Thread[Thread-14,5,ThreadGroup4], 0
Thread[Thread-14,5,ThreadGroup4], 1
Thread[Thread-14,5,ThreadGroup4], 2
Thread[Thread-8,4,ThreadGroup3], 1
Thread[Thread-8,4,ThreadGroup3], 2
Thread[Thread-13,4,ThreadGroup3], 0
Thread[Thread-13,4,ThreadGroup3], 1
Thread[Thread-13,4,ThreadGroup3], 2
Thread[Thread-12,3,ThreadGroup2], 0
Thread[Thread-12,3,ThreadGroup2], 1
Thread[Thread-12,3,ThreadGroup2], 2
Thread[Thread-11,2,ThreadGroup1], 0
Thread[Thread-11,2,ThreadGroup1], 1
Thread[Thread-11,2,ThreadGroup1], 2
Thread[Thread-6,2,ThreadGroup1], 0
Thread[Thread-6,2,ThreadGroup1], 1
Thread[Thread-6,2,ThreadGroup1], 2
Thread[Thread-10,1,ThreadGroup0], 0
Thread[Thread-10,1,ThreadGroup0], 1
Thread[Thread-10,1,ThreadGroup0], 2
Thread[Thread-5,1,ThreadGroup0], 0
Thread[Thread-5,1,ThreadGroup0], 1
Thread[Thread-5,1,ThreadGroup0], 2
运行testThreadGroup2:
Thread[Thread-0,5,ThreadGroup], 0
Thread[Thread-0,5,ThreadGroup], 1
Thread[Thread-0,5,ThreadGroup], 2
Thread[Thread-1,5,ThreadGroup]
Thread[Thread-2,5,ThreadGroup]
Thread[Thread-1,5,ThreadGroup], 0
Thread[Thread-1,5,ThreadGroup], 1
Thread[Thread-1,5,ThreadGroup], 2
Thread[Thread-2,5,ThreadGroup], 0
Thread[Thread-2,5,ThreadGroup], 1
Thread[Thread-2,5,ThreadGroup], 2