Java中怎么使用线程组

发布时间:2022-03-04 17:13:07 作者:iii
来源:亿速云 阅读:131

这篇文章主要介绍“Java中怎么使用线程组”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java中怎么使用线程组”文章能帮助大家解决问题。

Java中线程组(ThreadGroup类)

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

Java中怎么使用线程组

用户创建的所有线程都属于指定线程组,如果没有显式指定属于哪个线程组,那么该线程就属于默认线程组(即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.线程关联线程组:多级关联

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

Java中怎么使用线程组

代码示例如下:

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中怎么使用线程组”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. java高并发系列 - 第8天:线程组
  2. Java中线程组与未处理异常的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:java怎么解决猴子吃桃问题

下一篇:Java中关键字的知识点有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》