java 线程组_Java并发 之 线程组 ThreadGroup 介绍

线程组介绍

线程组(ThreadGroup)简单来说就是一个线程集合。线程组的出现是为了更方便地管理线程。

线程组是父子结构的,一个线程组可以集成其他线程组,同时也可以拥有其他子线程组。从结构上看,线程组是一个树形结构,每个线程都隶属于一个线程组,线程组又有父线程组,这样追溯下去,可以追溯到一个根线程组——System线程组。

下面介绍一下线程组树的结构:JVM创建的system线程组是用来处理JVM的系统任务的线程组,例如对象的销毁等。

system线程组的直接子线程组是main线程组,这个线程组至少包含一个main线程,用于执行main方法。

main线程组的子线程组就是应用程序创建的线程组。

你可以在main方法中看到JVM创建的system线程组和main线程组:

public static void main(String[] args) {

ThreadGroup mainThreadGroup=Thread.currentThread().getThreadGroup();

ThreadGroup systenThreadGroup=mainThreadGroup.getParent();

System.out.println("systenThreadGroup name = "+systenThreadGroup.getName());

System.out.println("mainThreadGroup name = "+mainThreadGroup.getName());

}

console输出:

systenThreadGroup name = system

mainThreadGroup name = main

一个线程可以访问其所属线程组的信息,但不能访问其所属线程组的父线程组或者其他线程组的信息。

线程组的构造

java.lang.ThreadGroup提供了两个构造函数:

下面演示一下这两个构造函数的用法:

public static void main(String[] args) {

ThreadGroup subThreadGroup1 = new ThreadGroup("subThreadGroup1");

ThreadGroup subThreadGroup2 = new ThreadGroup(subThreadGroup1, "subThreadGroup2");

System.out.println("subThreadGroup1 parent name = " + subThreadGroup1.getParent().getName());

System.out.println("subThreadGroup2 parent name = " + subThreadGroup2.getParent().getName());

}

console输出:

subThreadGroup1 parent name = main

subThreadGroup2 parent name = subThreadGroup1

ThreadGroup方法介绍

ThreadGroup提供了很多有用的方法,下面提供了这些方法的简要介绍,以及部分方法的使用示例。

查看线程组信息

下面演示了查看当前线程组的信息。

public static void list(){

ThreadGroup tg = new ThreadGroup ("subgroup 1");

Thread t1 = new Thread (tg, "thread 1");

Thread t2 = new Thread (tg, "thread 2");

Thread t3 = new Thread (tg, "thread 3");

tg = new ThreadGroup ("subgroup 2");

Thread t4 = new Thread (tg, "my thread");

tg = Thread.currentThread ().getThreadGroup ();

int agc = tg.activeGroupCount ();

System.out.println ("Active thread groups in " + tg.getName () + " thread group: " + agc);

tg.list ();

}

输出如下:

Active thread groups in main thread group: 2

java.lang.ThreadGroup[name=main,maxpri=10]

Thread[main,5,main]

java.lang.ThreadGroup[name=subgroup 1,maxpri=10]

java.lang.ThreadGroup[name=subgroup 2,maxpri=10]

终止线程组中的所有线程

一个线程应由其他线程来强制中断或停止,而是应该由线程自己自行停止。

因此 Thread.currentThread().stop(), Thread.currentThread().suspend(), Thread.currentThread().resume() 都已经被废弃了。

interrupt() 方法的作用是通知线程应该中断了,具体到底中断还是继续运行,由被通知的线程处理。

public class ThreadGroupExampleInterrupt {

public static void main(String[] args) {

// Start two threads MyThread mt = new MyThread();

mt.setName("A");

mt.start();

mt = new MyThread();

mt.setName("B");

mt.start();

// Wait 2 seconds try {

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

// Interrupt all methods in the same thread group as the main thread Thread.currentThread().getThreadGroup().interrupt();

}

//一个启动以后进入等待,直到被interrupt的线程 static class MyThread extends Thread {

public void run() {

synchronized ("A") {

System.out.println(getName() + " about to wait.");

try {

"A".wait();

} catch (InterruptedException e) {

System.out.println(getName() + " interrupted.");

}

System.out.println(getName() + " terminating.");

}

}

}

}

执行main方法输出:

A about to wait.

B about to wait.

A interrupted.

A terminating.

B interrupted.

B terminating.

总结

本节介绍了线程组(ThreadGroup)的概念,及其结构和构造函数,并演示了使用线程组方便地管理组内线程的几个方法。

本节是并发系列教程的一节,更多相关教程可以访问文章后面的链接。

后续会有更多关于并发编程的知识点的介绍,并且会结合企业项目进行实战介绍,欢迎继续关注。

Links

作者资源

相关资源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值