文章目录
- 一、介绍
- 二、线程组特性
- 1、关联性
- (1)一级关联性
- (2)多级关联性
- 2、自动归属属性
- 3、根线程组
- 三、线程组作用
- 1、统一异常处理机制
一、介绍
Java线程组(ThreadGroup)是一种用于组织和管理线程的机制。它允许将多个线程组织成一个单元,从而更容易进行管理和控制。线程组的主要作用包括组织、控制、监视和安全性。通过线程组,可以将相似或相关的线程放在同一个组内,便于管理;可以对整个线程组执行操作,如挂起、恢复、中断等;可以获取线程组的状态信息,如活动线程数、线程组名称等;还可以用于设置安全性策略,限制组内线程的权限。ThreadGroup的实现的接口、构造函数和属性信息如下:
-
private final ThreadGroup parent:指向父线程组的引用。
-
String name:线程组的名字,用于标识和区分不同线程组。
-
int maxPriority:定义了该线程组内所有线程允许的最大优先级,当线程试图设置高于此值的优先级时,系统会将其自动调整为组内的最大优先级。
-
boolean daemon:指示线程组是否为守护线程组,子线组程将继承这一属性,若为true,则当所有非守护线程终止后,守护线程也将结束。
线程组可以形成一个层次结构,其中每个线程组都可以包含其他线程组和线程。
总的来说,Java线程组是一种强大的工具,用于有效地组织、管理和控制多线程环境中的线程行为。通过合理地使用线程组,可以提高程序的并发性和可维护性,从而更好地利用系统资源。
二、线程组特性
1、关联性
(1)一级关联性
线程直属于某一线程组,例如:
package chatpter03;
import java.util.concurrent.TimeUnit;
public class TestThreadGroup01 {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
System.out.println("running thread: " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
thread1.start();
String name = thread1.getThreadGroup().getName();
System.out.println(Thread.currentThread().getName() + "的 thread group name: " + name);
String parentName = thread1.getThreadGroup().getParent().getName();
System.out.println("parent thread gourp name: " + parentName);
}
}
运行结果:
main的 thread group name: main
running thread: Thread-0
parent thread gourp name: system
Process finished with exit code 0
(2)多级关联性
线程组中的线程对象可以继续创建其他线程组,并形成一个层次结构。
package chatpter03;
import java.util.concurrent.TimeUnit;
public class TestThreadGroup02 {
public static void main(String[] args) {
ThreadGroup newThreadGroup = new ThreadGroup("newThreadGroup");
Thread thread1 = new Thread(newThreadGroup, () -> {
System.out.println("running thread: " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
Thread thread2 = new Thread(newThreadGroup, () -> {
System.out.println("running thread: " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
thread1.start();
thread2.start();
String thread1GroupName = thread1.getThreadGroup().getName();
String thread2GroupName = thread2.getThreadGroup().getName();
System.out.println(thread1.getName() + "的 thread group name: " + thread1GroupName);
System.out.println(thread2.getName() + "的 thread group name: " + thread2GroupName);
String parentName1 = thread1.getThreadGroup().getParent().getName();
String parentName = thread2.getThreadGroup().getParent().getName();
System.out.println(thread1GroupName + " parent thread group name: " + parentName1);
System.out.println(thread1GroupName + " parent thread group name: " + parentName);
}
}
运行结果:
Thread-0的 thread group name: newThreadGroup
Thread-1的 thread group name: newThreadGroup
running thread: Thread-1
running thread: Thread-0
newThreadGroup parent thread group name: main
newThreadGroup parent thread group name: main
Process finished with exit code 0
2、自动归属属性
当您创建一个线程对象时,如果当前线程存在一个线程组,新创建的线程对象将自动成为当前线程所属线程组的一员
3、根线程组
main根线程组的名称为system
运行结果:
main的 thread group name: main
running thread: Thread-0
Process finished with exit code 0
三、线程组作用
1、统一异常处理机制
package chatpter03;
public class TestThreadGroup04 {
public static void main(String[] args) {
ThreadGroup newThreadGroup = new ThreadGroup("newThreadGroup") {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println(t.getName() + " throw exception: " + e.getMessage());
}
};
Thread thread1 = new Thread(newThreadGroup, () -> {
System.out.println("running thread: " + Thread.currentThread().getName());
throw new RuntimeException("throw my exception");
});
Thread thread2 = new Thread(newThreadGroup, () -> {
System.out.println("running thread: " + Thread.currentThread().getName());
int a = 1 / 0;
});
thread1.start();
thread2.start();
}
}
运行结果:
running thread: Thread-0
running thread: Thread-1
Thread-0 throw exception: throw my exception
Thread-1 throw exception: / by zero
Process finished with exit code 0