当前位置:网站首页>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();
}
}
}
边栏推荐
- If you‘re running pod install manually, make sure flutter pub get is executed first.
- U++4 接口 学习笔记
- 拿到PMP认证带来什么改变?
- 腾讯云数据库公有云市场稳居TOP 2!
- Is PMP really useful?
- No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
- STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
- DFS, BFS and traversal search of Graphs
- Redis如何实现多可用区?
- PMP证书有没有必要续期?
猜你喜欢
【愚公系列】2022年7月 Go教学课程 005-变量
torch optimizer小解析
[opencv] image morphological operation opencv marks the positions of different connected domains
Sublime tips
qt 简单布局 盒子模型 加弹簧
How to choose an offer and what factors should be considered
Function pointer and pointer function in C language
window定时计划任务
Techniques d'utilisation de sublime
Weebly移动端网站编辑器 手机浏览新时代
随机推荐
Sublime tips
最长公共子序列(LCS)(动态规划,递归)
What changes will PMP certification bring?
Timer创建定时器
动态生成表格
The execution order of return in JS' try catch finally
Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
Complete code of C language neural network and its meaning
JS 的 try catch finally 中 return 的执行顺序
[opencv] image morphological operation opencv marks the positions of different connected domains
Array initialization of local variables
HarmonyOS第四次培训
U++4 接口 学习笔记
最长回文子串(动态规划)
批量归一化(标准化)处理
Comparison between thread and runnable in creating threads
QT控件样式系列(一)之QSlider
STM32F103 realize IAP online upgrade application
Markdown editor
腾讯云数据库公有云市场稳居TOP 2!