当前位置:网站首页>创建线程池的四种方式
创建线程池的四种方式
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
单例执行,这个线程池只有一个线程,这个线程可以在线程异常后重启一个线程替代原来线程继续去执行
边栏推荐
- Generation of tables and contingency tables (cross tables) of R language factor data: use the summary function to analyze the list, view the chi square test results, and judge whether the two factor v
- leetcode-深度优先与广度优先遍历
- Poj3275 ranking the cows
- Record a fake login of cookie
- SLAM论文合集
- DDoS protection with iptables
- Postgresql14安装及主从配置
- SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
- 彻底掌握二分查找
- Socket类关于TCP字符流编程的理解学习
猜你喜欢

30天刷题训练(一)

strcmp、strstr、memcpy、memmove的实现

基于NoneBot2的qq机器人配置记录

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

对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解

30 day question brushing plan (III)

DXF reading and writing: align the calculation of the position of the dimension text in the middle and above

Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"

Chapter 6 support vector machine

Istio四之故障注入和链路追踪
随机推荐
word打字时后面的字会消失是什么原因?如何解决?
Merge table rows - three levels of for loop traversal data
Using fail2ban to protect web servers from DDoS Attacks
Lyscript get previous and next instructions
Analyzing the principle of DNS resolution in kubernetes cluster
多线程与高并发(三)—— 源码解析 AQS 原理
Continuous (integration -- & gt; delivery -- & gt; deployment)
Cool operation preheating! Code to achieve small planet effect
MySQL开发技巧——视图
R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
Security assurance is based on software life cycle -istio authentication mechanism
ES6 what amazing writing methods have you used
DOJP1520星门跳跃题解
R语言ggplot2可视化:可视化散点图并为散点图中的数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点标签互相重叠(自定义指定字体类型font family)
.net for subtraction, intersection and union of complex type sets
Istio四之故障注入和链路追踪
Tutorial on the principle and application of database system (058) -- MySQL exercise (2): single choice question
leetcode(442)数组中重复的数据
DDoS protection with iptables
R语言因子数据的表格和列联表(交叉表)生成:使用summay函数分析列表查看卡方检验结果判断两个因子变量是否独立(使用卡方检验验证独立性)