线程组
概述 目录
1.线程组概念
2.一级关联
3.多级关联
4.线程组自动归属
5.批量管理线程
第1节 线程组的概念
把线程归属到一个线程组中,线程组中含有:
1. 线程
2. 线程组
线程组可以批量管理线程或线程组对象,有效地对线程或线程组对象进行组织。
第2节 一级关联
一级关联:
父对象中有子对象,但并不创建孙对象。
创建一个线程组,将部分线程归属到该组中,以此来对零散的线程对象进行有效的管理。
public class TestThread implements Runnable {
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("ThreadName = " + Thread.currentThread().getName());
Thread.sleep(3000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class OneLevelDemo {
public static void main(String[] args) {
TestThread testThread1 = new TestThread();
TestThread testThread2 = new TestThread();
ThreadGroup threadGroup = new ThreadGroup("新建线程组1");
Thread t0 = new Thread(threadGroup, testThread1);
Thread t1 = new Thread(threadGroup, testThread2);
t0.start();
t1.start();
System.out.println("活动的线程数为:" + threadGroup.activeCount());
System.out.println("线程组的名称为:" + threadGroup.getName());
}
}
ThreadName = Thread-0
ThreadName = Thread-1
活动的线程数为:2
线程组的名称为:新建线程组1
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-1
ThreadName = Thread-0
第3节 多级关联
多级关联:
父对象中有子对象,子对象中再创建子对象买也就出现了子孙的效果了。
这种写法在开发中不太常见,因为线程树如果设计得复杂反而不利于线程对象的管理,不过JDK确实提供了多级关联的线程树结构。
public class LevelsDemo {
public static void main(String[] args) {
ThreadGroup threadGroup1 = new ThreadGroup("线程组1");
ThreadGroup threadGroup2
= new ThreadGroup(threadGroup1, "线程组2");
ThreadGroup threadGroup3
= new ThreadGroup(threadGroup1, "线程组3");
TestThread testThread1 = new TestThread();
TestThread testThread2 = new TestThread();
TestThread testThread3 = new TestThread();
Thread t0 = new Thread(threadGroup1, testThread1);
Thread t1 = new Thread(threadGroup2, testThread2);
Thread t2 = new Thread(threadGroup3, testThread3);
t0.start();
t1.start();
t2.start();
System.out.println("threadGroup1线程组的名称为:" + threadGroup1.getName());
System.out.println("threadGroup1活动的线程数为:" + threadGroup1.activeCount());
System.out.println("threadGroup1活动的线程组数为:" + threadGroup1.activeGroupCount());
System.out.println("threadGroup2线程组的名称为:" + threadGroup2.getName());
System.out.println("threadGroup2活动的线程数为:" + threadGroup2.activeCount());
System.out.println("threadGroup3线程组的名称为:" + threadGroup3.getName());
System.out.println("threadGroup3活动的线程数为:" + threadGroup3.activeCount());
}
}
ThreadName = Thread-0
ThreadName = Thread-2
ThreadName = Thread-1
threadGroup1线程组的名称为:线程组1
threadGroup1活动的线程数为:3
threadGroup1活动的线程组数为:2
threadGroup2线程组的名称为:线程组2
threadGroup2活动的线程数为:1
threadGroup3线程组的名称为:线程组3
threadGroup3活动的线程数为:1
ThreadName = Thread-0
ThreadName = Thread-2
ThreadName = Thread-1
ThreadName = Thread-2
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-2
第4节 线程组自动归属
public class ThreadGroupAuto {
public static void main(String[] args) {
System.out.println("当前线程:" + Thread.currentThread().getName()
+ ", 所属线程组:" + Thread.currentThread().getThreadGroup().getName()
+ ", 线程组中有线程组数量:" + Thread.currentThread().getThreadGroup()
.activeGroupCount());
// 没有指定线程组,那么自动归到当前线程所属的线程组中
ThreadGroup group = new ThreadGroup("新的组");
System.out.println("线程组中有线程组数量:"
+ Thread.currentThread().getThreadGroup().activeGroupCount());
}
}
当前线程:main, 所属线程组:main, 线程组中有线程组数量:0
线程组中有线程组数量:1
第5节 批量管理线程
public class BatchThread extends Thread {
public BatchThread(ThreadGroup tg, String name) {
super(tg, name);
}
@Override
public void run() {
System.out.println("ThreadName = " + Thread.currentThread().getName() +
"开始死循环了");
while (!this.isInterrupted()) {
}
System.out.println("ThreadName = " + Thread.currentThread().getName() +
"结束了");
}
}
public class ThreadGroupBatchDemo {
public static void main(String[] args) throws InterruptedException {
int batch = 5;
ThreadGroup threadGroup = new ThreadGroup("我的线程组");
for (int i = 0; i < batch; i++) {
BatchThread batchThread
= new BatchThread(threadGroup, "线程" + i);
batchThread.start();
}
Thread.sleep(10000);
threadGroup.interrupt();
System.out.println("调用了ThreadGroup.interrupt()方法");
}
}
ThreadName = 线程0开始死循环了
ThreadName = 线程3开始死循环了
ThreadName = 线程4开始死循环了
ThreadName = 线程2开始死循环了
ThreadName = 线程1开始死循环了
调用了ThreadGroup.interrupt()方法
ThreadName = 线程0结束了
ThreadName = 线程1结束了
ThreadName = 线程4结束了
ThreadName = 线程3结束了
ThreadName = 线程2结束了