Abstract

Why need thread pool, did not want to understand this problem, look at the multi-threaded pool source are useless, first to know what thread pool technology to solve the problem, in order to understand the source code, because all the code is to solve the actual engineering problems.

The problem

Ask a few questions and see if you know, or if you don’t, maybe you don’t know enough about thread pools, or maybe you don’t know enough about thread pools. Then this article will be useful to you, please read on

  • Can the number of threads in the thread pool be 1?
  • Is there a need for a thread pool of 1
  • What is the difference between two thread pools with five core threads and one pool with ten core threads
  • How do you manage thread pools in an application
  • What is the difference between thread pooling and message queuing

Thread Pool Principle

When we were studying C in college, we would write a main function to the end and hand in homework. Controller -> Service -> DAO is OK for MVC. But writing code at work is about solving real problems. When you write the code and the user finds that your interface is too slow, what do you do?

  1. Asynchronously, you respond, you return an intermediate result, and then asynchronously, you return the result
  2. Concurrent, multiple threads to execute.

The thread pool mentioned in this article is mainly to improve the efficiency of program execution from the dimension of 1

Producer-consumer model

Friends with certain work experience are familiar with peak cutting and valley filling of message queue and system decoupling. So thread pools, are they peak clipping or not? After asynchrony, all tasks are placed in a queue. That is, producer -> container -> consumer. The following figure

manageability

Thread pooling seems to be the perfect solution to our problem, so what does it take? CPU resources are limited, thread creation costs, our Java application process resources are limited after all. You can’t create an infinite pool of threads in your application. So we need to manage thread pools.

Generally, for simple applications, we can use a singleton thread pool, so that all tasks in the application use the same thread pool. This prevents some junior programmers from creating thread pools randomly in the application, which causes thread resources to be tight and occupy too many resources.

When tasks are complex in an application, we need to use the idea of divide-and-conquer to isolate the thread pool. Some are CPU intensive, some are IO intensive; The importance of the task also has the degree of severity; The execution time of tasks also varies. We need to create different thread pools for these tasks to improve efficiency.

conclusion

Thread pooling is an asynchronous technique that improves response times by pre-creating threads/asynchronous processing. At the same time, unified allocation of thread resources can reduce the problem of repeated creation of threads, improve thread utilization, and centralized management is conducive to effective control of resources and prevention of abuse.

Pay attention to [abbot’s temple] and begin the path of technical practice with the abbot