当前位置:网站首页>线程池的创建与使用
线程池的创建与使用
2022-07-06 23:09:00 【洋啊桑815】
public ThreadPoolExecutor(int corePoolSize,核心线程的数量
int maximumPoolSize, 该线程池支持的最大线程数量
long keepAliveTime, 指定零时线程存活的时间
TimeUnit unit, 指定存活时间的单位 秒 分 时 天
BlockingQueue<Runnable> workQueue, 指定任务队列
ThreadFactory threadFactory, 指定在那个线程工厂创建线程
RejectedExecutionHandler handler) 指定线程忙 任务满时,新任务怎么办?
import java.util.concurrent.*;
public class TestDamo {
public static void main(String[] args) {
/*
* public ThreadPoolExecutor(int corePoolSize,核心线程的数量
int maximumPoolSize, 该线程池支持的最大线程数量
long keepAliveTime, 指定零时线程存活的时间
TimeUnit unit, 指定存活时间的单位 秒 分 时 天
BlockingQueue<Runnable> workQueue, 指定任务队列
ThreadFactory threadFactory, 指定在那个线程工厂创建线程
RejectedExecutionHandler handler) 指定线程忙 任务满时,新任务怎么办?
* */
ExecutorService pool=
new ThreadPoolExecutor
(3,5,8,
TimeUnit.SECONDS,new ArrayBlockingQueue<>(6),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
MyRunable myRunable=new MyRunable();
pool.execute(myRunable);//执行Runable
/*
* Future<T>submit (Callable<T>task) 执行Callable
* void shutdown()等待任务执行完毕后关闭线程
* List<Runable>shutdownNow() 立刻关闭线程,停止正在执行的方法,并返回未执行的任务
* */
pool.shutdown();
}
}
class MyRunable implements Runnable{
@Override
public void run() {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(2000);
System.out.println("线程运行:"+(i+1));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}边栏推荐
猜你喜欢

JDBC link Oracle reference code

U++ 元数据说明符 学习笔记

y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)

Markdown编辑器

Ansible overview and module explanation (you just passed today, but yesterday came to your face)

offer如何选择该考虑哪些因素

3GPP信道模型路损基础知识

Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)

Monitoring cannot be started after Oracle modifies the computer name

【问道】编译原理
随机推荐
Auto.js 获取手机所有app名字
5G VoNR+之IMS Data Channel概念
How to package the parsed Excel data into objects and write this object set into the database?
Error: No named parameter with the name ‘foregroundColor‘
torch optimizer小解析
STM32 encapsulates the one key configuration function of esp8266: realize the switching between AP mode and sta mode, and the creation of server and client
QT控件样式系列(一)之QSlider
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
A simple and beautiful regression table is produced in one line of code~
If you‘re running pod install manually, make sure flutter pub get is executed first.
R descriptive statistics and hypothesis testing
最全常用高数公式
史上最全学习率调整策略lr_scheduler
window定时计划任务
JDBC link Oracle reference code
Servicemesh mainly solves three pain points
[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
Tree map: tree view - draw covid-19 array diagram
3.基金的类型
Monitoring cannot be started after Oracle modifies the computer name