当前位置:网站首页>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
边栏推荐
- Open the past and let's start over.
- Installation and use of Apache bench (AB pressure test tool)
- Basic operation of sequence table
- Nature medicine reveals individual risk factors of coronary artery disease
- Iron and intestinal flora
- Leetcode003 -- judge whether an integer is a palindrome number
- 第四章 --- 了解标准设备文件、过滤器和管道
- 针对NFT的网络钓鱼
- AWS EKS 部署要点以及控制台与eksctl创建的差异
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
猜你喜欢

AWS EKS添加集群用户或IAM角色

Practice and exploration of knowledge map visualization technology in meituan

Recursive call -- Enumeration of permutations

win10, mysql-8.0.26-winx64.zip 安装
![[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN](/img/c5/3b3f9cf9a39bf14a68ac100294ca6c.png)
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN

Installation and deployment of Flink and wordcount test

229. Find mode II

Effects of antibiotics on microbiome and human health

递归调用--排列的穷举

520. Detect capital letters
随机推荐
Record the blind injection script
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
The last day of 2021 is the year of harvest.
[pytoch foundation] torch Split() usage
Innovative practice of short video content understanding and generation technology in meituan
Improving 3D object detection with channel wise transformer
MYSQL50道基础练习题
Practice and exploration of knowledge map visualization technology in meituan
Go 语言中的 logger 和 zap 日志库
Recommended scheme for national production of electronic components for wireless charging
Open the past and let's start over.
Go反射法则
383. Ransom letter
Recommended scheme of national manufactured electronic components for intelligent electronic scales
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
A new method for evaluating the quality of metagenome assembly - magista
unity摄像机旋转带有滑动效果(自转)
Leetcode004 -- Roman numeral to integer
leetcode006--查找字符串数组中的最长公共前缀
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)