当前位置:网站首页>Thread pool learning
Thread pool learning
2022-06-13 06:23:00 【bearstwu】
In the previous blogs, I have described the general idea of threads and the new problems in dealing with some special multithreads , When I want to deal with threading problems , I can't keep creating and maintaining threads , So what we need to do at this time is to create a new thing , There are many or enough threads in it , When I need to deal with the need to open up a new thread , I can get threads to handle this in this place , After processing, you can put the thread back to its previous position . In order to achieve this goal , Predecessors created a new concept called thread pool .
Make a simple analogy , raw , Officially, threads are the smallest unit of process execution . A thread pool is literally a stack of threads put together . For example , Threads are equivalent to small workers , Thread pool is equivalent to engineering team . I used to do tasks A, Hire a small worker to come here , Do the task B, Hire a small worker to come here ...., In this case, there will be a problem , It takes time and energy for me to recruit small workers , If the task is smaller , It'll be done in a minute , Then I spent half a day trying to hire a small worker. It took me longer than I did , Then I'm not losing money . therefore , In order to solve the problem of my frequent recruitment , We need to build an engineering team , So we have the concept of thread pool . With the engineering team , Now the way we work has changed , Mission A coming , I hired a small worker , Mission B coming , I hired a small worker , Then came the task C, Small worker a just finished , I will directly assign it to the first class worker , This time I won't have to hire . If all the tasks are done , I will directly disband the engineering team . This is the general treatment
So make a summary , As a thread pool , What I want to do or what I need to do for it
First of all , Analogy to the above example , I need to change the elements “ pay ” operation , That is to give them certain resources
second , I am responsible for it , Expand the number of threads to a point where you don't need to expand again
Third , When implementing this thread pool , Work efficiency should be improved
Fourth , If you can , These threads should be managed , Avoid miscalculation in threads
So how to create a thread pool
You don't have to deal with this too much , You can create it directly
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(new Runnable(){});but Executors Class in i As a tool class of thread pool , Of course, there is not only one tool for processing thread pools. There is only one method for processing thread pools , see Eecutors You can get the following kinds of thread pool processing from the source code of
newCachedThreadPool Create a cacheable thread pool , If the thread pool length exceeds processing requirements , Free threads can be recycled flexibly , If there is no recovery , New thread .
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
newFixedThreadPool Create a fixed-length thread pool , You can control the maximum number of concurrent threads , The exceeded thread will wait in the queue .
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}newScheduledThreadPool Create a fixed-length thread pool , Support regular and periodic task execution .
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
newSingleThreadExecutor Create a single threaded thread pool , It only uses a single worker thread to perform tasks
Now let's use the official language to explain what a thread pool is
- It's really just a A container that holds multiple threads , The threads can be reused , Eliminating the frequent creation of Thread objects ,-- No need to create threads repeatedly and consume too much resources .
Remember one principle here
Thread is the basic unit of system independent scheduling and dispatching , Process is the basic unit for computer to allocate and schedule resources in the system
Similar to thread pool , We can expand many other pools , For example, memory pool , Database pool, etc , What matters is not the name of the pool , It's the interconnected ideas .
边栏推荐
- 线程池学习
- Custom view - extensible collapsexpendview
- Differences among concurrent, parallel, serial, synchronous and asynchronous
- Uni app upload file
- Wechat applet (pull-down refresh data) novice to
- Relationship between fragment lifecycle and activity
- [JS] handwriting call(), apply(), bind()
- [var const let differences]
- USB debugging assistant 20181018 (v1.3)
- Commit specification
猜你喜欢

Dart class inherits and implements mixed operators

Failed to extract manifest from apk: processexception:%1 is not a valid Win32 Application.

Binary search

无刷直流电机矢量控制(四):基于滑模观测器的无传感器控制

Echart histogram: echart implements stacked histogram

Echart line chart: multiple line charts show only one line at a time

Download and installation of universal player potplayer, live stream m3u8 import

Fichier local second Search Tool everything
![[DP 01 backpack]](/img/be/1e5295684ead652eebfb72ab0be47a.jpg)
[DP 01 backpack]

RFID process management solution for electroplating fixture
随机推荐
线程相关点
【MySQL】基础知识小复习
Data conversion analysis tool
Common websites and tools
Wechat applet (pull-down refresh data) novice to
欧姆龙平替国产大货—JY-V640半导体晶元盒读写器
[JS] array de duplication
【案例】一个超级好用的工具 —— 给程序员用的计算器
Multiple reception occurs in the uniapp message delivery
Dragon Boat Festival wellbeing, use blessing words to generate word cloud
Wechat applet development (requesting background data and encapsulating request function)
华为开发者认证与DevEco Studio编译器下载
【新手上路常见问答】关于技术管理
Turn to 2005
The jadx decompiler can decompile jars and apks
Wechat applet: click the event to obtain the current device information (basic)
微信小程序:点击事件获取当前设备信息(基础)
Download and installation of universal player potplayer, live stream m3u8 import
You should consider upgrading via
超有范的 logo 在线设计制作工具