当前位置:网站首页>Record the ThreadPoolExecutor main thread waiting for sub threads
Record the ThreadPoolExecutor main thread waiting for sub threads
2022-04-23 04:41:00 【Z-hhhhh】
In the use of ThreadPoolExecutor There was a problem starting the thread pool : Cannot have the main thread wait for the child thread to complete before continuing .
I found many methods on the Internet, such as :
while Loop polling
Thread Class join Method
synchronized lock
CountDownLatch
Future
BlockingQueue
CyclicBarrier
LockSupport
Finally, it is found that none of them apply to ThreadPoolExecutor.
It took a long time to solve it .
The case is as follows
package com.siger;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
/** * @author zwh * @date 2022/2/16 */
public class threadTest {
static class MyRunable implements Runnable {
@Override
public void run() {
System.out.println("-------" + Thread.currentThread().getName() + " Operating -----------");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
System.out.println("-------" + Thread.currentThread().getName() + " Operating -----------");
for (int i = 0; i < 100; i++) {
System.out.print(i + "\t");
}
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 8 + 2, 500L, TimeUnit.MICROSECONDS, new LinkedBlockingDeque<>(), threadFactory);
for (int i = 0; i < 20; i++) {
//******* Key points of the problem ******
Future<?> future = executor.submit(new MyRunable());
try {
future.get();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
executor.shutdown();
System.out.println("-------" + Thread.currentThread().getName() + " Operating -----------");
for (int i = 200; i < 400; i++) {
System.out.print(i + "\t");
}
long endTime = System.currentTimeMillis();
long usedTime = (endTime - startTime) / 1000;
System.out.println("----------- Total time taken " + usedTime + "-------------");
}
}
The above is my plan , The test was successfully completed .
If any big man has a better way , Please give me some advice .
The previous methods are not applicable ThreadPoolExecutor, Although this creation method is good , Not easy to appear OOM, Maybe I'm not used to it , Still need to study more .
About ThreadPoolExecutor For other questions, see another article
Alibaba tips : Creating threads manually works better
版权声明
本文为[Z-hhhhh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220559122179.html
边栏推荐
- leetcode009--用二分查找在数组中搜索目标值
- 数据孤岛是什么?为什么2022年仍然存在数据孤岛?
- AWS EKS添加集群用户或IAM角色
- unity摄像机旋转带有滑动效果(自转)
- leetcode006--查找字符串数组中的最长公共前缀
- AWS eks add cluster user or Iam role
- Practice and exploration of knowledge map visualization technology in meituan
- 2019 is coming to an end, the longest day.
- A new method for evaluating the quality of metagenome assembly - magista
- SQL statement for adding columns in MySQL table
猜你喜欢
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
C language: spoof games
AWS eks add cluster user or Iam role
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
520.检测大写字母
第四章 --- 了解标准设备文件、过滤器和管道
Installation and use of Apache bench (AB pressure test tool)
MySQL queries users logged in for at least N consecutive days
随机推荐
Go reflection - go language Bible learning notes
Leetcode - > 1 sum of two numbers
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
Flink's important basics
Leetcode008 -- implement strstr() function
shell wc (统计字符数量)的基本使用
Leetcode004 -- Roman numeral to integer
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Go 语言中的 logger 和 zap 日志库
leetcode003--判断一个整数是否为回文数
Phishing for NFT
PIP3 installation requests Library - the most complete pit sorting
leetcode006--查找字符串数组中的最长公共前缀
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
Redis command Encyclopedia
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
229. Find mode II
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
Migrate from MySQL database to AWS dynamodb