当前位置:网站首页>线程池的创建与使用
线程池的创建与使用
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();
}
}
}边栏推荐
- How to design API interface and realize unified format return?
- Two methods of chromosome coordinate sequencing
- If you‘re running pod install manually, make sure flutter pub get is executed first.
- [736. LISP syntax parsing]
- R descriptive statistics and hypothesis testing
- 01 machine learning related regulations
- JS input and output
- JDBC link Oracle reference code
- 拿到PMP认证带来什么改变?
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
猜你喜欢

Markdown编辑器

Tree map: tree view - draw covid-19 array diagram

The sooner you understand the four rules of life, the more blessed you will be

Function pointer and pointer function in C language

Monitoring cannot be started after Oracle modifies the computer name

U++ metadata specifier learning notes

SQL injection HTTP header injection

How to design API interface and realize unified format return?

JS also exports Excel

JS variable plus
随机推荐
一个酷酷的“幽灵”控制台工具
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
STM32 system timer flashing LED
《五》表格
全链路压测:影子库与影子表之争
How to package the parsed Excel data into objects and write this object set into the database?
CentOS 7.9安装Oracle 21c历险记
C语言中函数指针与指针函数
Run the command once per second in Bash- Run command every second in Bash?
Analyse approfondie de kubebuilder
Error: No named parameter with the name ‘foregroundColor‘
IMS data channel concept of 5g vonr+
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
Inventory host list in ansible (I wish you countless flowers and romance)
c语言神经网络基本代码大全及其含义
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
Talk about the importance of making it clear
Servicemesh mainly solves three pain points
高数中值定理总结
U++ 游戏类 学习笔记