当前位置:网站首页>为了速率创建线程池,启动核心线程
为了速率创建线程池,启动核心线程
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);
}
}
边栏推荐
- UE4 highlight official reference value
- Unicode私人使用区域(Private Use Areas)
- Greenplus enterprise deployment
- Process and concept of process
- [beauty of software engineering - column notes] "one question and one answer" issue 3 | 18 common software development problem-solving strategies
- Inclination sensor accuracy calibration test
- Some simple uses of crawler requests Library
- STM32 detection signal frequency
- [noi simulation] computational geometry (convex hull, violence, and search set)
- Intelligent shelf safety monitoring system
猜你喜欢
[cryptography experiment] 0x00 install NTL Library
Ansible (automation software)
Unity multiplayer online framework mirror learning record (I)
Stm8s003 domestic substitute for dp32g003 32-bit microcontroller chip
[robomaster] control RM motor from scratch (2) -can communication principle and electric regulation communication protocol
New energy shared charging pile management and operation platform
Explanation and closing method of server 135, 137, 138, 139, 445 and other ports
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification
Inclination sensor is used for long-term monitoring of communication tower and high-voltage tower
阿里巴巴政委体系-第一章、政委建在连队上
随机推荐
20 hacker artifacts
分段分页以及段页结合
[cryptography experiment] 0x00 install NTL Library
Charging pile charging technology new energy charging pile development
Privacy is more secure in the era of digital RMB
Qt/PyQt 窗口类型与窗口标志
Proteus simulation based on msp430f2491
[beauty of software engineering - column notes] 25 | what methods can improve development efficiency?
Qt/pyqt window type and window flag
Shell script - global variables, local variables, environment variables
SQL 面试碰到的一个问题
Crawl notes
Dynamically load data
网络安全之安全基线
Alibaba political commissar system - Chapter 4: political commissars are built on companies
Limitations of push down analysis
Simple calculator wechat applet project source code
Proteus simulation based on msp430f2491 (realize water lamp)
Explanation and closing method of server 135, 137, 138, 139, 445 and other ports
Inclination sensor accuracy calibration test