当前位置:网站首页>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 .
边栏推荐
- How to view APK version number from apk
- 欧姆龙平替国产大货—JY-V640半导体晶元盒读写器
- The title of the WebView page will be displayed in the top navigation bar of the app. How to customize
- Time complexity and space complexity
- Alibaba cloud OSS file download cannot be resumed at a breakpoint
- [written examination questions of meituan]
- [case] a super easy-to-use tool -- a calculator for programmers
- [one · data 𞓜 simple implementation of the leading two-way circular linked list]
- Echart histogram: stacked histogram displays value
- SSM integration
猜你喜欢
The web server failed to start Port 7001 was already in use
本地文件秒搜工具 Everything
Wechat applet (get location)
Intelligent digital asset management helps enterprises win the post epidemic Era
Super model logo online design and production tool
《MATLAB 神经网络43个案例分析》:第11章 连续Hopfield神经网络的优化——旅行商问题优化计算
The boys x pubgmobile linkage is coming! Check out the latest game posters
RN Metro packaging process and sentry code monitoring
Learning records countless questions (JS)
【新手上路常见问答】关于技术管理
随机推荐
Learning records countless questions (JS)
华为开发者认证与DevEco Studio编译器下载
Fragment lifecycle
Local file search tool everything
Basic knowledge of knowledge map
Omron Ping replaces the large domestic product jy-v640 semiconductor wafer box reader
Recent problems
Uniapp mobile terminal uses canvas to draw background convex arc
Time complexity and space complexity
【新手上路常见问答】一步一步理解程序设计
High burst solution 2
自定义View —— 可伸展的CollapsExpendView
USB debugging assistant 20181018 (v1.3)
Data conversion analysis tool
[var const let differences]
Wechat applet uploads pictures (preview deletion limits the size and number of pictures)
Custom view
Turn to 2005
Differences among concurrent, parallel, serial, synchronous and asynchronous
[2022 college entrance examination season] what I want to say as a passer-by