当前位置:网站首页>Learning and understanding of thread pool (with code examples)
Learning and understanding of thread pool (with code examples)
2022-06-26 09:54:00 【Graffiti CC】
java Simple instance of thread pool creation
Thread pool
1、 Threads are divided into kernel threads KLT And user threads ULT Android in :ULT(APP Thread used ) KLT( system-used )
2、 our jvm Most virtual machines are kernel threads klt
This can compile my Hello.java file , take for The cycle value is adjusted to 2000, function , Then open your windows System task manager , You can see that there are 2000 more system processes , This is it. klt. If ult It will not be displayed in the task management of the system .
3、 Thread pool creation TheadModel file ,Task Is the task that each thread runs .
4、 Thread pool is suitable for a single task with less workload ( Less time-consuming ) And the number of tasks is large .
5、 About KLT Multithreaded execution ,cpu Each thread will be allocated inconsistent execution time , If the thread does not finish executing within the execution time , Then his current state will be saved to memory ( It is used to save the status when the thread task that has not been completed in the allocated time is interrupted The container of ), When cpu After executing all the task queues , And then take out the unfinished thread from the memory to continue execution .
6、 I am here Android I haven't come into contact with , Understand the principle of a process pool .
7、 The number of tasks that the thread pool can store , Do not exceed (maximumPoolSize+workQueu) The number of .
8、 Thread pool runtime calls execute( ) Save task , Call after storing the task shutdown( ) To make the thread pool not accept new tasks after all current tasks are executed , And become TIDYING, Then go to the thread pool and terminate completely
The five states of the thread pool
1、RUNNING
(1) Status description : The thread pool is in RUNNING In the state of , Ability to take on new tasks , And processing the added tasks .
(2) State switching : The initialization state of the thread pool is RUNNING. let me put it another way , The thread pool is created once , It's in RUNNING state , And the number of tasks in the thread pool is 0!
2、 SHUTDOWN
(1) Status description : The thread pool is in SHUTDOWN In the state of , Don't take on new tasks , But can handle added tasks .
(2) State switching : Call the thread pool shutdown() Interface , Thread pools are made up of RUNNING -> SHUTDOWN.
3、STOP
(1) Status description : The thread pool is in STOP In the state of , Don't take on new tasks , Do not process the added tasks , And it interrupts the task being processed .
(2) State switching : Call the thread pool shutdownNow() Interface , Thread pools are made up of (RUNNING or SHUTDOWN ) -> STOP.
4、TIDYING
(1) Status description : When all tasks have terminated ,ctl Records of the ” Number of tasks ” by 0, The thread pool becomes TIDYING state . When the thread pool becomes TIDYING In the state of , The hook function is executed terminated().terminated() stay ThreadPoolExecutor Class is empty , If the user wants to change in the thread pool TIDYING when , Deal with it accordingly ; It can be overloaded terminated() Function to implement .
(2) State switching : When the thread pool is in SHUTDOWN State, , When the blocking queue is empty and the tasks executing in the thread pool are also empty , It will be by SHUTDOWN -> TIDYING.
When the thread pool is in STOP State, , The tasks executed in the thread pool are null , It will be by STOP -> TIDYING.
5、 TERMINATED
(1) Status description : The thread pool terminates completely , It becomes TERMINATED state .
(2) State switching : The thread pool is in TIDYING In the state of , After execution terminated() after , It will be by TIDYING -> TERMINATED.
git Warehouse ( Internal instance , Click the text to jump to )
Just send it directly
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** * TheadModel */
public class TheadModel {
public static void main(String[] args) {
// Core thread pool size , The maximum number of threads , If you don't have a job, how long will you be free , Provide blocking queues at full load , Thread temp ( Threads that exceed the maximum allowed threads will be placed here )
// Blocking queues :
// 1、 No matter how high the concurrency , There is always only one thread that can queue in and out Thread safe queues
// bounded || unbounded
// When the queue is full Only out of line operations can be performed , All queue operations must wait It's blocked
// If the queue is empty, you can only enter the queue , All out of line operations must wait , It's blocked
/** public ThreadPoolExecutor(int corePoolSize, The number of threads remaining in the pool even when idle , Unless set allowCoreThreadTimeOut int maximumPoolSize, The maximum number of threads allowed in the pool long keepAliveTime, When the number of threads is greater than the core , This is the maximum amount of time an extra idle thread can wait for a new task before terminating . TimeUnit unit, keepAliveTime Time unit of parameter new ArrayBlockingQueue<Runnable>(5) The queue used to save tasks before executing them . The queue will only be saved execute Method submitted Runnable Mission . ) */
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,3,60,TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(5));
for (int i = 0; i < 20; i++) {
threadPoolExecutor.execute(new Task(i));
}
threadPoolExecutor.shutdown();
}
}
public class Task implements Runnable{
private int nov;
public Task(int nov){
this.nov=nov;
}
public void run(){
// System.out.println(" The thread executing the current task is :"+Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (Exception e) {
}
System.out.println(" I'm on a mission :"+nov+" I'm doing ...");
}
}
边栏推荐
- Jupyter Notebook遇到的问题
- c语言语法基础之——函数嵌套、递归 小程序斐波那契之和、阶乘
- Halcon photometric stereoscopic
- c语言语法基础之——指针(字符、一维数组) 学习
- Opencv depthframe - > pointcloud causes segmentation fault!
- Enter the page input box to automatically obtain the focus
- 异常记录-23
- 2021-11-29 轨迹规划五次多项式
- SQL modification of table structure
- SQL advanced tutorial
猜你喜欢

How does flutter transfer parameters to the next page when switching pages?

jz2440---使用uboot燒錄程序

VI summary of common commands
QPM performance monitoring components - General

2021-11-29 quintic polynomial of trajectory planning

做测试需要知道的内容——url、弱网、接口、自动化、

jz2440---使用uboot烧录程序

Several connection query methods of SQL (internal connection, external connection, full connection and joint query)

WGCLOUD的web ssh服务端口是多少

2021-11-29 轨迹规划五次多项式
随机推荐
Speed test of adding, deleting, modifying and querying 5million pieces of data in a single MySQL table
MySQL单表500万条数据增、删、改、查速度测试
Flutter's brain map notes are easy to find and search!
教你用shell脚本检测服务器程序是否在运行
定制拦截器
logback
使用递归或while循环获取父/子层级结构的名称
Introduction to QPM
SQL modification of table structure
LeetCode 498. Diagonal traversal
libgstreamer-1.0. so. 0: cannot open shared object file: No such file or directory
Jupyter Notebook遇到的问题
力扣------从数组中移除最大值和最小值
Why do some functions in the go standard library have only signatures but no function bodies?
The basis of C language grammar -- factoring by function applet
Leetcode refers to offer II 091 Paint house - modify in place
My creation anniversary
Several connection query methods of SQL (internal connection, external connection, full connection and joint query)
logback
Cento7.7 elk installation simple record