当前位置:网站首页>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();
}
}
}
边栏推荐
- Decorator basic learning 02
- 与利润无关的背包问题(深度优先搜索)
- 最长不下降子序列(LIS)(动态规划)
- What changes will PMP certification bring?
- The most complete learning rate adjustment strategy in history LR_ scheduler
- 最长回文子串(动态规划)
- qt 简单布局 盒子模型 加弹簧
- Array initialization of local variables
- 带你遨游银河系的 10 种分布式数据库
- 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
猜你喜欢
随机推荐
3.基金的类型
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
U++4 interface learning notes
U++ game learning notes
【opencv】图像形态学操作-opencv标记不同连通域的位置
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
线程同步的两个方法
【二叉树】二叉树寻路
Mysql database (basic)
Linkedblockingqueue source code analysis - initialization
Full link voltage test: the dispute between shadow database and shadow table
高数中值定理总结
01 machine learning related regulations
【最佳网页宽度及其实现】「建议收藏」
DFS, BFS and traversal search of Graphs
最长不下降子序列(LIS)(动态规划)
SQL injection cookie injection
QT simple layout box model with spring
最长公共子序列(LCS)(动态规划,递归)
Clickhouse (03) how to install and deploy Clickhouse