并发API提供的一个有趣功能是可以将多个线程组成一个组。

这样我们就能将这一组线程看做一个单元并且提供改组内线程对象的读取操作。例如

你有一些线程在执行同样的任务并且你想控制他们,不考虑有多少个线程仍在运行,一个打断动作将会打断所有组内线程的执行。



Java提供了一个ThreadGroup类来表示一组线程。一个线程组对象由多个线程对象和另一个线程组对象组成,从而形成树状的线程结构。

本例中,我们将学习使用ThreadGroup对象开发一个简单例子。我们有10个线程模拟搜索(随机睡眠一段时间),并且其中一个执行结束,我们将打断其余线程。

Result.java
package com.dylan.thread.ch1.c10.task;

/**
* Class that stores the result of the search
*
*/
public class Result { /**
* Name of the Thread that finish
*/
private String name; /**
* Read the name of the Thread
* @return The name of the Thread
*/
public String getName() {
return name;
} /**
* Write the name of the Thread
* @param name The name of the Thread
*/
public void setName(String name) {
this.name = name;
} }
SearchTask.java
package com.dylan.thread.ch1.c10.task;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit; /**
* Class that simulates a search operation
*
*/
public class SearchTask implements Runnable { /**
* Store the name of the Thread if this Thread finish and is not interrupted
*/
private Result result; /**
* Constructor of the class
* @param result Parameter to initialize the object that stores the results
*/
public SearchTask(Result result) {
this.result=result;
} @Override
public void run() {
String name=Thread.currentThread().getName();
System.out.printf("Thread %s: Start\n",name);
try {
doTask();
result.setName(name);
} catch (InterruptedException e) {
System.out.printf("Thread %s: Interrupted\n",name);
return;
}
System.out.printf("Thread %s: End\n",name);
} /**
* Method that simulates the search operation
* @throws InterruptedException Throws this exception if the Thread is interrupted
*/
private void doTask() throws InterruptedException {
Random random=new Random((new Date()).getTime());
int value=(int)(random.nextDouble()*100);
System.out.printf("Thread %s: %d\n",Thread.currentThread().getName(),value);
TimeUnit.SECONDS.sleep(value);
} }
Main.java

package com.dylan.thread.ch1.c10.core;

import com.dylan.thread.ch1.c10.task.Result;
import com.dylan.thread.ch1.c10.task.SearchTask; import java.util.concurrent.TimeUnit; public class Main { /**
* Main class of the example
* @param args
*/
public static void main(String[] args) { // Create a ThreadGroup
ThreadGroup threadGroup = new ThreadGroup("Searcher");
Result result=new Result(); // Create a SeachTask and 10 Thread objects with this Runnable
SearchTask searchTask=new SearchTask(result);
for (int i=0; i<10; i++) {
Thread thread=new Thread(threadGroup, searchTask);
thread.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
} // Write information about the ThreadGroup to the console
System.out.printf("Number of Threads: %d\n",threadGroup.activeCount());
System.out.printf("Information about the Thread Group\n");
threadGroup.list(); // Write information about the status of the Thread objects to the console
Thread[] threads=new Thread[threadGroup.activeCount()];
threadGroup.enumerate(threads);
for (int i=0; i<threadGroup.activeCount(); i++) {
System.out.printf("Thread %s: %s\n",threads[i].getName(),threads[i].getState());
} // Wait for the finalization of the Threadds
waitFinish(threadGroup); // Interrupt all the Thread objects assigned to the ThreadGroup
threadGroup.interrupt();
} /**
* Method that waits for the finalization of one of the ten Thread objects
* assigned to the ThreadGroup
* @param threadGroup
*/
private static void waitFinish(ThreadGroup threadGroup) {
while (threadGroup.activeCount()>9) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }

运行结果:

Thread Thread-0: Start
Thread Thread-0: 59
Thread Thread-1: Start
Thread Thread-1: 50
Thread Thread-2: Start
Thread Thread-2: 29
Thread Thread-3: Start
Thread Thread-3: 19
Thread Thread-4: Start
Thread Thread-4: 47
Thread Thread-5: Start
Thread Thread-5: 38
Thread Thread-6: Start
Thread Thread-6: 66
Thread Thread-7: Start
Thread Thread-7: 57
Thread Thread-8: Start
Thread Thread-8: 83
Thread Thread-9: Start
Thread Thread-9: 74
Number of Threads: 10
Information about the Thread Group
java.lang.ThreadGroup[name=Searcher,maxpri=10]
    Thread[Thread-0,5,Searcher]
    Thread[Thread-1,5,Searcher]
    Thread[Thread-2,5,Searcher]
    Thread[Thread-3,5,Searcher]
    Thread[Thread-4,5,Searcher]
    Thread[Thread-5,5,Searcher]
    Thread[Thread-6,5,Searcher]
    Thread[Thread-7,5,Searcher]
    Thread[Thread-8,5,Searcher]
    Thread[Thread-9,5,Searcher]
Thread Thread-0: TIMED_WAITING
Thread Thread-1: TIMED_WAITING
Thread Thread-2: TIMED_WAITING
Thread Thread-3: TIMED_WAITING
Thread Thread-4: TIMED_WAITING
Thread Thread-5: TIMED_WAITING
Thread Thread-6: TIMED_WAITING
Thread Thread-7: TIMED_WAITING
Thread Thread-8: TIMED_WAITING
Thread Thread-9: TIMED_WAITING
Thread Thread-3: End
Thread Thread-1: Interrupted
Thread Thread-8: Interrupted
Thread Thread-9: Interrupted
Thread Thread-0: Interrupted
Thread Thread-7: Interrupted
Thread Thread-6: Interrupted
Thread Thread-4: Interrupted
Thread Thread-2: Interrupted
Thread Thread-5: Interrupted

Java并发编程实例--10.使用线程组的更多相关文章

  1. [Java并发编程(一)] 线程池 FixedThreadPool vs CachedThreadPool ...

    [Java并发编程(一)] 线程池 FixedThreadPool vs CachedThreadPool ... 摘要 介绍 Java 并发包里的几个主要 ExecutorService . 正文 ...

  2. 2、Java并发编程:如何创建线程

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  3. 原创】Java并发编程系列2:线程概念与基础操作

    [原创]Java并发编程系列2:线程概念与基础操作 伟大的理想只有经过忘我的斗争和牺牲才能胜利实现. 本篇为[Dali王的技术博客]Java并发编程系列第二篇,讲讲有关线程的那些事儿.主要内容是如下这 ...

  4. Java并发编程:如何创建线程?

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  5. Java 并发编程——Executor框架和线程池原理

    Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...

  6. 【转】Java并发编程:如何创建线程?

    一.Java中关于应用程序和进程相关的概念 在Java中,一个应用程序对应着一个JVM实例(也有地方称为JVM进程),一般来说名字默认是java.exe或者javaw.exe(windows下可以通过 ...

  7. [Java并发编程(二)] 线程池 FixedThreadPool、CachedThreadPool、ForkJoinPool?为后台任务选择合适的 Java executors

    [Java并发编程(二)] 线程池 FixedThreadPool.CachedThreadPool.ForkJoinPool?为后台任务选择合适的 Java executors ... 摘要 Jav ...

  8. Java 并发编程——Executor框架和线程池原理

    Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...

  9. Java并发编程 | 从进程、线程到并发问题实例解决

    计划写几篇文章讲述下Java并发编程,帮助一些初学者成体系的理解并发编程并实际使用,而不只是碎片化的了解一些Synchronized.ReentrantLock等技术点.在讲述的过程中,也想融入一些相 ...

  10. Java并发编程之深入理解线程池原理及实现

    Java线程池在实际的应用开发中十分广泛.虽然Java1.5之后在JUC包中提供了内置线程池可以拿来就用,但是这之前仍有许多老的应用和系统是需要程序员自己开发的.因此,基于线程池的需求背景.技术要求了 ...

随机推荐

  1. 银河麒麟安装多版本gcc的方式方法

    银河麒麟安装多版本gcc的方式方法 背景 最近想升级一下gcc 但是发现自己编译的话非常麻烦 记得之前CentOS7的时候有一个scl的处理 发现CentOS8 已经没有scl的仓库了 简单验证了一下 ...

  2. [转帖]tidb关闭sql_mode=ONLY_FULL_GROUP_BY模式

    报错: 1 of ORDER BY clause is not in SELECT list, references column 'xxx' which is not in SELECT list ...

  3. [转帖]利用Python调用outlook自动发送邮件

    ↓↓↓欢迎关注我的公众号,在这里有数据相关技术经验的优质原创文章↓↓↓ 使用Python发送邮件有两种方式,一种是使用smtp调用邮箱的smtp服务器,另一种是直接调用程序直接发送邮件.而在outlo ...

  4. DPText-DETR: 基于动态点query的场景文本检测,更高更快更鲁棒 | 京东探索研究院

    针对场景文本检测任务,近期基于DEtection TRansformer (DETR) 框架预测控制点的研究工作较为活跃.在基于DETR的检测器中,query的构建方式至关重要,现有方法中较为粗糙的位 ...

  5. 大模型应用开发:为产品创建一个AI客服/智能助手

    欢迎阅读本系列文章!我将带你一起探索如何使用OpenAI API来开发GPT应用.无论你是编程新手还是资深开发者,都能在这里获得灵感和收获. 本文将继续展示AI助手的开发方式,在OpenAPI中它的名 ...

  6. linux服务器cup100%问题排查

    一.出现问题在发现公司门禁服务无法开门的第一时间,去线上服务器上查看了一下进程的运行情况,具体运行如下: 第一次在查看的时候发现并没有我需要的服务entranceguard进程(图片是后续截图的) 二 ...

  7. 策略模式学习,使用go实现策略模式

    策略模式 定义 优点 缺点 使用场景 代码实现 策略模式和工厂模式的区别 参考 策略模式 定义 策略模式定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化,不会影响到客户端的使用 ...

  8. 【Java】ArrayList线程不安全的坑

    问题复现: 使用Java的steam().paralleStream(),foreach()方法向ArrayList添加数据,导致ArrayList中出现空值,代码如下: public static ...

  9. 大语言模型的预训练4:指示学习Instruction Learning详解以及和Prompt Learning,In-content Learning区别

    大语言模型的预训练[4]:指示学习Instruction Learning:Entailment-oriented.PLM oriented.human-oriented详解以及和Prompt Lea ...

  10. [转发]MySQL安装配置教程(超级详细、保姆级)

    MySQL安装配置教程(超级详细.保姆级)_SoloVersion的博客-CSDN博客_mysql安装配置教程一. 下载MySQLMysql官网下载地址https://downloads.mysql. ...