浅析Java中线程组(ThreadGroup类)

2022-10-16,,,

java中使用threadgroup类来代表线程组,表示一组线程的集合,可以对一批线程和线程组进行管理。可以把线程归属到某一个线程组中,线程组中可以有线程对象,也可以有线程组,组中还可以有线程,这样的组织结构有点类似于树的形式,如图所示。

 

用户创建的所有线程都属于指定线程组,如果没有显式指定属于哪个线程组,那么该线程就属于默认线程组(即main线程组)。默认情况下,子线程和父线程处于同一个线程组。

此外,只有在创建线程时才能指定其所在的线程组,线程运行中途不能改变它所属的线程组,也就是说线程一旦指定所在的线程组就不能改变。

二.为什么要使用线程组

1.安全

同一个线程组的线程是可以相互修改对方的数据的。但如果在不同的线程组中,那么就不能“跨线程组”修改数据,可以从一定程度上保证数据安全。

2.批量管理

可以批量管理线程或线程组对象,有效地对线程或线程组对象进行组织或控制。

三.线程组使用示例

1.线程关联线程组:一级关联

所谓一级关联就是父对象中有子对象,但并不创建孙对象。比如创建一个线程组,然后将创建的线程归属到该组中,从而对这些线程进行有效的管理。代码示例如下:

public class threadgrouptest {
 public static void main(string[] args) {
 threadgroup rootthreadgroup = new threadgroup("root线程组");
 thread thread0 = new thread(rootthreadgroup, new mrunnable(), "线程a");
 thread thread1 = new thread(rootthreadgroup, new mrunnable(), "线程b");
 thread0.start();
 thread1.start();
 }
}
class mrunnable implements runnable {
 @override
 public void run() {
 while (!thread.currentthread().isinterrupted()) {
 system.out.println("线程名: " + thread.currentthread().getname() 
+ ", 所在线程组: " + thread.currentthread().getthreadgroup().getname()) ;
 try {
 thread.sleep(1000);
 } catch (interruptedexception e) {
 e.printstacktrace();
 }
 }
 }
}
复制代码

执行结果如下:

线程名: 线程a, 所在线程组: root线程组
线程名: 线程b, 所在线程组: root线程组
复制代码

2.线程关联线程组:多级关联

所谓的多级关联就是父对象中有子对象,子对象中再创建孙对象也就出现了子孙的效果了。比如使用下图第二个构造方法,将子线程组归属到某个线程组,再将创建的线程归属到子线程组,这样就会有线程树的效果了。

 

代码示例如下:

public class threadgrouptest {
 public static void main(string[] args) {
 threadgroup rootthreadgroup = new threadgroup("root线程组");
 thread thread0 = new thread(rootthreadgroup, new mrunnable(), "线程a");
 thread thread1 = new thread(rootthreadgroup, new mrunnable(), "线程b");
 thread0.start();
 thread1.start();
 threadgroup threadgroup1 = new threadgroup(rootthreadgroup, "子线程组");
 thread thread2 = new thread(threadgroup1, new mrunnable(), "线程c");
 thread thread3 = new thread(threadgroup1, new mrunnable(), "线程d");
 thread2.start();
 thread3.start();
 }
}
class mrunnable implements runnable {
 @override
 public void run() {
 while (!thread.currentthread().isinterrupted()) {
 system.out.println("线程名: " + thread.currentthread().getname()
 + ", 所在线程组: " + thread.currentthread().getthreadgroup().getname()
 + ", 父线程组: " + thread.currentthread().getthreadgroup().getparent().getname());
 try {
 thread.sleep(1000);
 } catch (interruptedexception e) {
 e.printstacktrace();
 }
 }
 }
}
复制代码

执行结果如下:

线程名: 线程a, 所在线程组: root线程组, 父线程组: main
线程名: 线程b, 所在线程组: root线程组, 父线程组: main
线程名: 线程c, 所在线程组: 子线程组, 父线程组: root线程组
线程名: 线程d, 所在线程组: 子线程组, 父线程组: root线程组
复制代码

3.批量管理组内线程

使用线程组自然是要对线程进行批量管理,比如可以批量中断组内线程,代码示例如下:

public class threadgrouptest {
 public static void main(string[] args) {
 threadgroup rootthreadgroup = new threadgroup("root线程组");
 thread thread0 = new thread(rootthreadgroup, new mrunnable(), "线程a");
 thread thread1 = new thread(rootthreadgroup, new mrunnable(), "线程b");
 thread0.start();
 thread1.start();
 threadgroup threadgroup1 = new threadgroup(rootthreadgroup, "子线程组");
 thread thread2 = new thread(threadgroup1, new mrunnable(), "线程c");
 thread thread3 = new thread(threadgroup1, new mrunnable(), "线程d");
 thread2.start();
 thread3.start();
 rootthreadgroup.interrupt();
 system.out.println("批量中断组内线程");
 }
}
class mrunnable implements runnable {
 @override
 public void run() {
 while (!thread.currentthread().isinterrupted()) {
 system.out.println("线程名: " + thread.currentthread().getname()
 + ", 所在线程组: " + thread.currentthread().getthreadgroup().getname()
 + ", 父线程组: " + thread.currentthread().getthreadgroup().getparent().getname());
 try {
 thread.sleep(1000);
 } catch (interruptedexception e) {
 e.printstacktrace();
 break;
 }
 }
 system.out.println(thread.currentthread().getname() + "执行结束");
 }
}
复制代码

执行结果如下:

线程名: 线程a, 所在线程组: root线程组, 父线程组: main
线程名: 线程b, 所在线程组: root线程组, 父线程组: main
线程名: 线程c, 所在线程组: 子线程组, 父线程组: root线程组
线程名: 线程d, 所在线程组: 子线程组, 父线程组: root线程组
批量中断组内线程
线程a执行结束
线程b执行结束
线程c执行结束
线程d执行结束
复制代码

本文只是对java中的threadgroup类进行了简单的介绍和使用示范,更多线程组的操作可以查看jdk api。

《浅析Java中线程组(ThreadGroup类).doc》

下载本文的Word格式文档,以方便收藏与打印。