当前位置:网站首页>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 .
边栏推荐
- Simple use of event bus
- Uniapp secondary encapsulates uview components, and the parent component controls display and hiding
- [MySQL] basic knowledge review
- Echart折线图:当多条折线存在相同name,图例仍全部显示
- Multiple reception occurs in the uniapp message delivery
- Uni app dynamic setting title
- MFS详解(六)——MFS Chunk Server服务器安装与配置
- Win10 drqa installation
- Notifyitemchanged flash back
- Fragment lifecycle
猜你喜欢

JS convert text to language for playback
![[2022 college entrance examination season] what I want to say as a passer-by](/img/d7/3813b944dc2df182455d475a3669fb.jpg)
[2022 college entrance examination season] what I want to say as a passer-by

【新手上路常见问答】一步一步理解程序设计

JVM基础

楊輝三角形詳解

推荐扩容工具,彻底解决C盘及其它磁盘空间不够的难题
![[DP 01 backpack]](/img/be/1e5295684ead652eebfb72ab0be47a.jpg)
[DP 01 backpack]

Recommend a capacity expansion tool to completely solve the problem of insufficient disk space in Disk C and other disks

【2022高考季】作为一个过来人想说的话

pthon 执行 pip 指令报错 You should consider upgrading via ...
随机推荐
USB status error and its cause (error code)
The web server failed to start Port 7001 was already in use
El form form verification
Logcat -b events and eventlogtags print the location correspondence of the events log in the code
Binary search
pthon 执行 pip 指令报错 You should consider upgrading via ...
[one · data 𞓜 simple implementation of the leading two-way circular linked list]
【新手上路常见问答】关于技术管理
Fichier local second Search Tool everything
Hbuilderx: installation of hbuilderx and its common plug-ins
The technical analysis of ERP systems of the two camps in the world has been picked up many times.
Uniapp secondary encapsulates uview components, and the parent component controls display and hiding
Waterfall flow layout of uni app Homepage
[JS] array de duplication
347. top k high frequency elements heap sort + bucket sort +map
[written examination questions of meituan]
The boys x pubgmobile linkage is coming! Check out the latest game posters
App performance test: (II) CPU
Learning records countless questions (JS)
JVM Foundation