当前位置:网站首页>创建线程池的四种方式
创建线程池的四种方式
2022-07-28 13:02:00 【学习微站】
newCachedThreadPool
创建可缓存的线程池,根据需要创建,以前存在重用他们
newFixedThreadPool
创建固定的线程池,可重用
newScheduledThredPool
创建一个线程池,安排计划执行(定期)
public static void main(String[] args) {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
scheduledThreadPool.schedule(new Runnable() {
@Override
public void run() {
System.out.println("等3秒");
}
}, 3, TimeUnit.SECONDS);
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("延迟一秒后每三秒执行一次");
}
}, 1, 3, TimeUnit.SECONDS);
scheduledThreadPool.scheduleAtFixedRate(() -> {
System.out.println("===============");
}, 1, 3, TimeUnit.SECONDS);
}
结果
延迟一秒后每三秒执行一次
===============
等3秒
延迟一秒后每三秒执行一次
===============
延迟一秒后每三秒执行一次
===============
延迟一秒后每三秒执行一次
===============
延迟一秒后每三秒执行一次
===============
newSingleThreadExecutor
单例执行,这个线程池只有一个线程,这个线程可以在线程异常后重启一个线程替代原来线程继续去执行
边栏推荐
- 正则表达式
- Analyzing the principle of DNS resolution in kubernetes cluster
- 111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server
- 7.依赖注入
- Duplicate data in leetcode (442) array
- Operator3 - design an operator
- Tutorial on the principle and application of database system (059) -- MySQL exercise questions: operation questions 1-10 (III)
- 修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...
- Tutorial on the principle and application of database system (058) -- MySQL exercise (2): single choice question
- R language Visual scatter diagram, geom using ggrep package_ text_ The repl function avoids overlapping labels between data points (add labels to specific areas of the visual image using the parameter
猜你喜欢

SQL daily practice (Niuke new question bank) - day 4: advanced operators

strcmp、strstr、memcpy、memmove的实现

Istio四之故障注入和链路追踪

安全保障基于软件全生命周期-Istio的授权机制

安全保障基于软件全生命周期-Istio的认证机制

7.依赖注入

Security assurance is based on software life cycle -psp application

安全保障基于软件全生命周期-NetworkPolicy应用

How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question

Qt5开发从入门到精通——第一篇概述
随机推荐
R language uses LM function to build multiple linear regression model, writes regression equation according to model coefficient, and uses conflict function to give 95% confidence interval of regressi
Chapter 6 support vector machine
Master several common sorting - Select Sorting
《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
掌握闭包,夯实基本功
产品经理:岗位职责表
记一次COOKIE的伪造登录
【LVGL事件(Events)】事件代码
30 day question brushing plan (II)
How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question
Continuous (integration -- & gt; delivery -- & gt; deployment)
作为一个程序员,如何高效的管理时间?
多线程与高并发(三)—— 源码解析 AQS 原理
Qt5开发从入门到精通——第一篇概述
Remember to use pdfbox once to parse PDF and obtain the key data of PDF
Thoroughly master binary search
Dojnoip201708 cheese solution
DXF读写:标注样式组码中文说明
安全保障基于软件全生命周期-PSP应用
Socket class understanding and learning about TCP character stream programming