当前位置:网站首页>线程池的三大方法
线程池的三大方法
2022-06-22 06:10:00 【酷小亚】
线程池的三大方法
Executors.newSingleThreadExecutor();单线程
Executors.newFixedThreadPool(5);固定线程个数
Executors.newCachedThreadPool();缓存池,可扩展
Executors.newSingleThreadExecutor(); 单线程
package pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** * Executors 工具类、3大方法 */
public class Demo01 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newSingleThreadExecutor();//单个线程
try {
for (int i = 0; i < 10; i++) {
//使用了线程池之后,使用线程池来创建线程
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+"OK!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//线程池用完,程序结束,关闭线程池
threadPool.shutdown();
}
}
}
Executors.newFixedThreadPool(5); 固定线程个数
package pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** * Executors 工具类、3大方法 */
public class Demo01 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(5); //创建一个固定的线程池的大小
try {
for (int i = 0; i < 10; i++) {
//使用了线程池之后,使用线程池来创建线程
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" OK!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//线程池用完,程序结束,关闭线程池
threadPool.shutdown();
}
}
}
Executors.newCachedThreadPool(); 缓存池,可扩展
1package pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** * Executors 工具类、3大方法 */
public class Demo01 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newCachedThreadPool();//可伸缩的,遇强则强,遇弱则弱
try {
for (int i = 0; i < 100; i++) {
//使用了线程池之后,使用线程池来创建线程
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" OK!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//线程池用完,程序结束,关闭线程池
threadPool.shutdown();
}
}
}
边栏推荐
- Single cell paper record (Part11) -- clustermap for multi-scale clustering analysis of spatial gene expression
- Serial port (RS - 232)
- 【雲計算重點複習】
- Ptrade trading program code - from zero to firm offer 19
- Single cell thesis record (Part12) -- unsupervised spatial embedded deep representation of spatial transcriptomics
- 论文实验记录(part1)--Detection ofnatural clusters via S-DBSCAN a Self-tuning version of DBSCAN
- Bat common batch script record
- 单细胞论文记录(part11)--ClusterMap for multi-scale clustering analysis of spatial gene expression
- 生信可视化(part2)--箱线图
- TCP连接细节问题
猜你喜欢

Flink核心功能和原理

Case analysis of terminal data leakage prevention

单细胞论文记录(part12)--Unsupervised Spatial Embedded Deep Representation of Spatial Transcriptomics

Shengxin visualization (Part2) -- box diagram

Signal output library

C#中的泛型

Single cell paper records (Part8) -- cell2location maps fine grained cell types in spatial transcriptomics

【CPU设计实战】数字逻辑电路设计基础(一)

关于MNIST线性模型矩阵顺序问题

Linear regression: least squares, Tellson estimation, RANSAC
随机推荐
Serial port (RS - 232)
单细胞文献学习(part2)--stPlus: a reference-based method for the accurate enhancement of ST
单细胞论文记录(part14)--CoSTA: unsupervised convolutional neural network learning for ST analysis
单球机器人动力学与控制研究
Breakthrough in rich device platform: dayu200 based on rk3568 enters the openharmony 3.1 release trunk
关于MNIST线性模型矩阵顺序问题
从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?
生信文献学习(part1)--PRECISE: a ... approach to transfer predictors of drug response from pre-clinical ...
从入门到精通之专家系统CLIPS(一)CLIPS初识与概述
相干声呐GeoSwath的综述
Single cell paper records (part10) -- computational challenges and opportunities in SRT data
402 string (Title: Sword finger offer58 ii. left rotation string, 28. implementation of strstr(), 459 Repeated substrings)
osg编译osgQt
TCP connection details
DataBricks从开源到商业化踩过的坑
Unity encrypts ASE game data
单细胞文献学习(part3)--DSTG: deconvoluting spatial transcriptomics data through graph-based AI
单细胞论文记录(part12)--Unsupervised Spatial Embedded Deep Representation of Spatial Transcriptomics
idea插件EasyCode的使用
Single cell paper record (Part14) -- costa: unsupervised revolutionary neural network learning for St analysis