当前位置:网站首页>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();
}
}
}
边栏推荐
- PMP证书有没有必要续期?
- Longest non descent subsequence (LIS) (dynamic programming)
- AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
- 01 machine learning related regulations
- Techniques d'utilisation de sublime
- [Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
- 【问道】编译原理
- 全链路压测:影子库与影子表之争
- AOSP ~Binder 通信原理 (一) - 概要
- Dynamically generate tables
猜你喜欢
sublime使用技巧
What changes will PMP certification bring?
批量归一化(标准化)处理
How to choose an offer and what factors should be considered
Analysis -- MySQL statement execution process & MySQL architecture
Error: No named parameter with the name ‘foregroundColor‘
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
动态生成表格
《四》表单
[736. LISP syntax parsing]
随机推荐
Auto.js 获取手机所有app名字
torch optimizer小解析
Auto. JS get all app names of mobile phones
Error: No named parameter with the name ‘foregroundColor‘
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
3. Type of fund
Techniques d'utilisation de sublime
NiO related knowledge points (I)
pmp真的有用吗?
y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
Analysis -- MySQL statement execution process & MySQL architecture
c语言神经网络基本代码大全及其含义
Knapsack problem unrelated to profit (depth first search)
QSlider of QT control style series (I)
STM32 system timer flashing LED
The most complete learning rate adjustment strategy in history LR_ scheduler
Test interview | how much can you answer the real test interview question of an Internet company?
When knative meets webassembly
Markdown editor
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)