当前位置:网站首页>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();
}
}
}
边栏推荐
- [736. LISP syntax parsing]
- 线程池的创建与使用
- Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
- y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
- Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
- 【二叉树】二叉树寻路
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
- Timer创建定时器
- 最全常用高数公式
猜你喜欢
记录一次压测经验总结
When knative meets webassembly
Basic knowledge of road loss of 3GPP channel model
SQL injection - secondary injection and multi statement injection
Analysis -- MySQL statement execution process & MySQL architecture
Decorator basic learning 02
[736. LISP syntax parsing]
Y58. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (Sany)
AOSP ~Binder 通信原理 (一) - 概要
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
随机推荐
Knapsack problem (01 knapsack, complete knapsack, dynamic programming)
Leetcode(46)——全排列
Leetcode(417)——太平洋大西洋水流问题
线程同步的两个方法
HarmonyOS第四次培训
QT simple layout box model with spring
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
Time complexity & space complexity
U++ metadata specifier learning notes
DBSync新增对MongoDB、ES的支持
Appium practice | make the test faster, more stable and more reliable (I): slice test
NiO related knowledge points (I)
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
[QT] custom control loading
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
LabVIEW在打开一个新的引用,提示内存已满
Inventory host list in ansible (I wish you countless flowers and romance)
SQL injection - secondary injection and multi statement injection
U++ game learning notes
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