当前位置:网站首页>Creation and use of thread pool
Creation and use of thread pool
2022-07-07 05:14:00 【Yang Asang 815】
public ThreadPoolExecutor(int corePoolSize, Number of core threads
int maximumPoolSize, The maximum number of threads supported by this thread pool
long keepAliveTime, Specify the time for the thread to survive at zero
TimeUnit unit, Specify the unit of survival time second branch when God
BlockingQueue<Runnable> workQueue, Specify task queue
ThreadFactory threadFactory, Specify which thread factory to create the thread
RejectedExecutionHandler handler) The specified thread is busy When the task is full , What about the new task ?
import java.util.concurrent.*;
public class TestDamo {
public static void main(String[] args) {
/*
* public ThreadPoolExecutor(int corePoolSize, Number of core threads
int maximumPoolSize, The maximum number of threads supported by this thread pool
long keepAliveTime, Specify the time for the thread to survive at zero
TimeUnit unit, Specify the unit of survival time second branch when God
BlockingQueue<Runnable> workQueue, Specify task queue
ThreadFactory threadFactory, Specify which thread factory to create the thread
RejectedExecutionHandler handler) The specified thread is busy When the task is full , What about the new task ?
* */
ExecutorService pool=
new ThreadPoolExecutor
(3,5,8,
TimeUnit.SECONDS,new ArrayBlockingQueue<>(6),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
MyRunable myRunable=new MyRunable();
pool.execute(myRunable);// perform Runable
/*
* Future<T>submit (Callable<T>task) perform Callable
* void shutdown() Wait for the thread to close after the task is executed
* List<Runable>shutdownNow() Close thread now , Stop the method being executed , And return the unimplemented task
* */
pool.shutdown();
}
}
class MyRunable implements Runnable{
@Override
public void run() {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(2000);
System.out.println(" Thread running :"+(i+1));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}边栏推荐
猜你喜欢

Function pointer and pointer function in C language

Weebly移动端网站编辑器 手机浏览新时代

SQL injection cookie injection

Error: No named parameter with the name ‘foregroundColor‘

基于Bevy游戏引擎和FPGA的双人游戏

Analysis -- MySQL statement execution process & MySQL architecture

Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)

Basic knowledge of road loss of 3GPP channel model

如何设计 API 接口,实现统一格式返回?

How to design API interface and realize unified format return?
随机推荐
ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
《五》表格
Is PMP really useful?
与利润无关的背包问题(深度优先搜索)
The founder has a debt of 1billion. Let's start the class. Is it about to "end the class"?
磁盘监控相关命令
U++ 游戏类 学习笔记
STM32 system timer flashing LED
Decorator basic learning 02
NiO related knowledge points (I)
When knative meets webassembly
Clickhouse (03) how to install and deploy Clickhouse
Servicemesh mainly solves three pain points
Dynamically generate tables
模拟线程通信
DFS,BFS以及图的遍历搜索
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
Markdown editor
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
创始人负债10亿,开课吧即将“下课”?