1 一个大型任务,可分成多个独立的子线程并发进行,最后等待所有的子线程执行结束然后继续往下执行,

使用场景比如

要查找某个用户的最近三个月的通话记录,起 3 个子线程,分别查找最近三个月的记录,然后通过

                int activeCount = tgroup.activeCount();
while ( activeCount > ) {
System.out.println("activeCount=" + activeCount );
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
activeCount = tgroup.activeCount();
}

阻塞住主线程,直到三个子线程全部执行结束,activeCount() == 0,继续玩下执行

2,一个大型任务,可分成多个独立的子线程并发进行,只要其中的某个子线程完成了任务条件,就算任务完成,则调用 threadGroup.interrupt() 方法 其他的子线程就可以停止了

使用场景如

用户表按用户ID分成了10个表,这时候想通过用户昵称找到这个用户,则这时候就可以使用threadGroup中的 interrupt 来实现了

 package com.zyguo.thread;

 public class SearchRunnable implements Runnable{
private int sleepTime;
public SearchRunnable( int sleepTime ){
this.sleepTime = sleepTime;
} @Override
public void run() {
try {
System.out.println( Thread.currentThread() + " begin search " );
Thread.sleep( sleepTime );
System.out.println( Thread.currentThread() + " end search, 耗时 " + this.sleepTime );
} catch (InterruptedException e) {
System.out.println( Thread.currentThread() + " 被中断 " );
//e.printStackTrace();
}
} }
package com.zyguo.thread;

import java.util.ArrayList;

public class ThreadGroup_interrupt {
public static void main(String[] args) {
int threadNum = ;
final ThreadGroup tgroup = new ThreadGroup("search-threadgroup");
ArrayList<Thread> tList = new ArrayList<>();
//定义10个线程
for( int i = ; i < threadNum; i++ ){
Thread t = new Thread( tgroup, new SearchRunnable( + i*) ,"search-thread-" + i);
tList.add( t );
t.start();
System.out.println("start thread = " + t );
} //监控线程的活动的子线程数
Thread t = new Thread( new Runnable() {
@Override
public void run() {
int activeCount = tgroup.activeCount();
while ( activeCount > ) {
System.out.println("activeCount=" + activeCount );
if( activeCount < ){
System.out.println("找到了需要的文件,开始终止其他的子线程" );
tgroup.interrupt();
break;
}
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
activeCount = tgroup.activeCount();
}
}
}); t.start(); }
}

结果如下

start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
Thread[search-thread-,,search-threadgroup] begin search
start thread = Thread[search-thread-,,search-threadgroup]
start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
start thread = Thread[search-thread-,,search-threadgroup]
Thread[search-thread-,,search-threadgroup] begin search
activeCount=
activeCount=
activeCount=
activeCount=
activeCount=
Thread[search-thread-,,search-threadgroup] end search, 耗时
activeCount=
Thread[search-thread-,,search-threadgroup] end search, 耗时
activeCount=
Thread[search-thread-,,search-threadgroup] end search, 耗时
activeCount=
Thread[search-thread-,,search-threadgroup] end search, 耗时
activeCount=
Thread[search-thread-,,search-threadgroup] end search, 耗时
activeCount=
Thread[search-thread-,,search-threadgroup] end search, 耗时
activeCount=
找到了需要的文件,开始终止其他的子线程
Thread[search-thread-,,search-threadgroup] 被中断
Thread[search-thread-,,search-threadgroup] 被中断
Thread[search-thread-,,search-threadgroup] 被中断
Thread[search-thread-,,search-threadgroup] 被中断

3,ThreadGroup 的使用场景以及用法的更多相关文章

  1. SharedPreferences 的另一种场景的用法

    SharedPreferences 的另一种场景的用法 昨天,下班在家想做什么来着,然后想用SharedPreferences存点数据,但是不知道咋地突然想到,SharedPreferences是应用 ...

  2. Ext JS学习第十六天 事件机制event(一) DotNet进阶系列(持续更新) 第一节:.Net版基于WebSocket的聊天室样例 第十五节:深入理解async和await的作用及各种适用场景和用法 第十五节:深入理解async和await的作用及各种适用场景和用法 前端自动化准备和详细配置(NVM、NPM/CNPM、NodeJs、NRM、WebPack、Gulp/Grunt、G

    code&monkey   Ext JS学习第十六天 事件机制event(一) 此文用来记录学习笔记: 休息了好几天,从今天开始继续保持更新,鞭策自己学习 今天我们来说一说什么是事件,对于事件 ...

  3. yii框架场景的用法

    1.在 model 里面定义一下场景 类名必须是 scenarios() public function scenarios() { return [ 'create' => ['title', ...

  4. ognl表达式应用场景和用法

    ognl表达式的用法和应用场景 1.配置文件 //书写方式是:'${@类全限定名@常量}' dic_city.type=${@com.imooc.constant.DictionaryConstant ...

  5. 第十五节:深入理解async和await的作用及各种适用场景和用法

    一. 同步VS异步 1.   同步 VS 异步 VS 多线程 同步方法:调用时需要等待返回结果,才可以继续往下执行业务 异步方法:调用时无须等待返回结果,可以继续往下执行业务 开启新线程:在主线程之外 ...

  6. 深入理解async和await的作用及各种适用场景和用法

    https://www.cnblogs.com/yaopengfei/archive/2018/07/02/9249390.html https://www.cnblogs.com/xianyudot ...

  7. RunLoop 总结:RunLoop的应用场景(一)

    参考资料 好的书籍都是值得反复看的,那好的文章,好的资料也值得我们反复看.我们在不同的阶段来相同的文章或资料或书籍都能有不同的收获,那它就是好文章,好书籍,好资料.关于iOS 中的RunLoop资料非 ...

  8. 深入分析@Transactional的用法

    关键词:事务, 编程式事务,声明式事务.spring 事务管理.AOP事务增强.@Transactional 在分析深入分析@Transactional的使用之前,我们先回顾一下事务的一些基本内容. ...

  9. python+selenium自动化软件测试(第4章):场景判断与封装

    4.1 显示等待WebDriverWait 前言:在脚本中加入太多的sleep后会影响脚本的执行速度,虽然implicitly_wait()这种隐式等待在一定程度上节省了很多时间.但是一旦页面上某些j ...

随机推荐

  1. bash's [ command & [[ keyword

    [bash's [ command & [[ keyword] [ (test) command: bash中的条件测试語句, [ condition ], 并不是一个語句, 而是一个命令, ...

  2. Excel VBA 获取按钮对象

    今天给同事写了两个VBA宏,并分别把宏赋给了两个按钮. 因为两个宏都是实现在两种显示方式之间切换,于是我想除了功能的实现外,还希望在切换到其中一种方式时,按钮上面的文字也可以跟着改变,起到提示作用. ...

  3. 【HDU5361】In Touch

    题意有n个人住在一条直线上,从左到右编号为1,2,3....n                                                                     ...

  4. labelimg

    ------------------------labelimg------------------------- cd /home/luo/TensorflowProject/labelImg py ...

  5. 14. Longest Common Prefix 最长的公共字符串开头

    [抄题]: Write a function to find the longest common prefix string amongst an array of strings. 在 " ...

  6. Openssl crl命令

    一.简介 crl命令用于处里PME或DER格式的CRL文件 二.语法 openssl crl [-inform PEM|DER] [-outform PEM|DER] [-text] [-in fil ...

  7. LinuxSystemProgramming-Syllabus

    Linux System Programming Syllabus

  8. code2800 送外卖

    首先,对图进行一次Floyd(g[][]是图) 1.dfs:(u是当前在的节点,d是已经走的路程) void dfs(int u,int d){ if(d>=ans)return; bool f ...

  9. xargs在linux中的使用详解-乾颐堂

    xargs在linux中是个很有用的命令,它经常和其他命令组合起来使用,非常的灵活. xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤 ...

  10. js失去焦点触发

    onblur="displayRest($(this))"