当前位置:网站首页>为了速率创建线程池,启动核心线程
为了速率创建线程池,启动核心线程
2022-07-29 08:15:00 【未来的农场主】
直接上代码
public static final int NUMBER_8 = 8;
private static final long KEEP_ALIVETIME = 600;
@Override
public void downCusAllInfo(String username,String Token,String orgid) throws InterruptedException {
// 创建线程池
int corePoolSize = NUMBER_8;
//corePoolSize:
//线程池的基本大小,即在没有任务需要执行的时候线程池的大小,并且只有在工作队列满了的情况下才会创建超出这个数量的线程。
//在刚刚创建ThreadPoolExecutor的时候,线程并不会立即启动,而是要等到有任务提交时才会启动,
调用了prestartCoreThread/prestartAllCoreThreads事先启动核心线程,再考虑keepAliveTime
int maximumPoolSize = NUMBER_8;
//maximumPoolSize:
//线程池中允许的最大线程数,线程池中的当前线程数目不会超过该值。如果队列中任务已满,并且当前线程个数小于maximumPoolSize,那么会创建新的线程来执行任务。
//largestPoolSize:该变量记录了线程池在整个生命周期中曾经出现的最大线程个数。线程池创建之后,可以调用setMaximumPoolSize()改变运行的最大线程的数目。
long keepAliveTime = KEEP_ALIVETIME;
//keepAliveTime的单位是纳秒,1s=1000000000ns,1秒等于10亿纳秒。
//keepAliveTime是线程池中空闲线程等待工作的超时时间。
//当线程池中线程数量大于corePoolSize(核心线程数量)或设置了allowCoreThreadTimeOut(是否允许空闲核心线程超时)时,
// 线程会根据keepAliveTime的值进行活性检查,一旦超时便销毁线程。否则,线程会永远等待新的工作。
TimeUnit unit = TimeUnit.SECONDS;
//常用的有界队列为 ArrayBlockingQueue,也可能是限定了范围的其他队列,创建方式如下:
BlockingQueue<Runnable> workQueue1 = new ArrayBlockingQueue<>(50000);
//也可能是限定了范围的其他队列,如
BlockingQueue<Runnable> workQueue2 = new LinkedBlockingQueue<>(50000);
//常用的无界队列为没有预定容量的 LinkedBlockingQueue,创建方式如下:
//优点:由于没有边界限制,缓冲队列能够存储更多的任务,可以平滑顺时大量请求。
//劣势:
//由于没有限制队列大小,其所占用的内存空间不可预知,有内存溢出的风险
//同时当任务平均提交速度大于平均处理速度时,有队列无限增长的风险
//由于队列无限增长,不会触发拒绝策略
//结论:在处理大量并发时,无界队列效果优于有界队列
//这里使用的无界队列
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
executor.prestartAllCoreThreads(); // 预启动所有核心线程
List<String> querycus = customerDao.querycuss(orgid);
System.out.println(JSON.toJSONString(querycus));
logger.info("档案条数:" + querycus.size());
for (int a = 0; a < querycus.size(); a++) {
String sfzh = querycus.get(a);
executor.execute(() -> {
try {
//循环调用downCusInfoOnes方法
downCusInfoOnes(username,sfzh,Token);
} catch (Exception e) {
e.printStackTrace();
logger.info("档案详情下载失败:" + sfzh);
}
});
}
//关闭线程池
executor.shutdown();
while (true) {
if (executor.isTerminated()) {
System.out.println("线程结束");
break;
}
Thread.sleep(SLEEP);
}
}边栏推荐
- New energy shared charging pile management and operation platform
- 数仓分层设计及数据同步问题,,220728,,,,
- Convert source package to RPM package
- Tcp/ip five layer reference model and corresponding typical devices and IPv6
- 数字人民币时代隐私更安全
- Random lottery turntable wechat applet project source code
- [密码学实验] 0x00 安装NTL库
- Very practical shell and shellcheck
- 简易计算器微信小程序项目源码
- [beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)
猜你喜欢

Simplefoc parameter adjustment 1-torque control

Application of explosion-proof inclination sensor in safe operation of LNG
![[beauty of software engineering - column notes]](/img/90/658f4d954c876bed2fccb431232e52.png)
[beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
![[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?](/img/65/79f876b62fa3db421e5038a2445b83.png)
[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?

Proteus simulation based on msp430f2491 (realize water lamp)

STM32 MDK (keil5) contents mismatch error summary

数仓分层设计及数据同步问题,,220728,,,,

亚马逊测评自养号是什么,卖家应该怎么做?

UE4 principle and difference between skylight and reflecting sphere

10 common software architecture modes
随机推荐
Tle5012b+stm32f103c8t6 (bluepill) reading angle data
Implementation of simple cubecap+fresnel shader in unity
Security baseline of network security
Use the cloud code to crack the problem of authentication code encountered during login
Ws2812b color lamp driver based on f407zgt6
Stm32ff030 replaces domestic MCU dp32g030
[noi simulation] computational geometry (convex hull, violence, and search set)
Simplefoc+platformio stepping on the path of the pit
[robomaster] control RM motor from scratch (2) -can communication principle and electric regulation communication protocol
Low cost 2.4GHz wireless transceiver chip -- ci24r1
sql判断语句的编写
A problem encountered in SQL interview
Dynamic thresholds buffer management in a shared buffer packet switch paper summary
随机抽奖转盘微信小程序项目源码
数仓分层设计及数据同步问题,,220728,,,,
C language interview preparation I (about understanding Department)
Vmstat memory consumption query
[beauty of software engineering - column notes] 27 | what is the core competitiveness of software engineers? (top)
Mysql rownum 实现
Unicode private use areas