测试代码:
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(); } } }
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
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