当前位置:网站首页>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
边栏推荐
- [paper reading] [3D target detection] point transformer
- zynq平台交叉编译器的安装
- leetcode006--查找字符串数组中的最长公共前缀
- Recursive call -- Enumeration of permutations
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
- Supplement: Annotation
- Error occurs when thymeleaf th: value is null
- Small volume Schottky diode compatible with nsr20f30nxt5g
- Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
- The last day of 2021 is the year of harvest.
猜你喜欢
Innovative practice of short video content understanding and generation technology in meituan
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Apache Bench(ab 压力测试工具)的安装与使用
[paper reading] [3D target detection] point transformer
Small volume Schottky diode compatible with nsr20f30nxt5g
A new method for evaluating the quality of metagenome assembly - magista
Bacterial infection and antibiotic use
Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
随机推荐
2020 is coming to an end, special and unforgettable.
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
Supplement: Annotation
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Redis 命令大全
Recommended scheme for national production of electronic components of wireless keyboard
520. Detect capital letters
Go反射法则
test
Inverse system of RC low pass filter
Eksctl deploying AWS eks
RC低通滤波器的逆系统
Bacterial infection and antibiotic use
Nature medicine reveals individual risk factors of coronary artery disease
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
Key points of AWS eks deployment and differences between console and eksctl creation
Leetcode - > 1 sum of two numbers
Coinbase:关于跨链桥的基础知识、事实和统计数据
win10, mysql-8.0.26-winx64.zip 安装