- Thread pools are ubiquitous for performance optimization
- 1.The performance problems that landlords encounter in the process of product development in peacetime, especially the recently popular micro-service architecture,
- web – java – The underlying data source (python or opensatck) for this scenario of front-and-back separation
- There will always be repeated calls to the same data source 100 times and 1000 times for business scenarios.
- In terms of performance, there will be interface delays, too slow, timeouts, and so on.
- 2.Use of thread pools to improve performance in this high concurrency scenario
Thread pools are ubiquitous for performance optimization
1.The performance problems that landlords encounter in the process of product development in peacetime, especially the recently popular micro-service architecture,
web – java – The underlying data source (python or opensatck) for this scenario of front-and-back separation
There will always be repeated calls to the same data source 100 times and 1000 times for business scenarios.
In terms of performance, there will be interface delays, too slow, timeouts, and so on.
Here is an example of a business scenario recently encountered by the landlord: Cloud computing must be related to knowing that we are in the process of providing vendors to customers (that is, physical machines); many vendors will be managed, here we will introduce the host group;
Host group is used to manage the existence of vendors. Often in the process of using, there will be insufficient resources. At this time, we need to migrate the vendors with insufficient resources to other host groups, so as to achieve the rational use of virtual machine resources, which is in the process of migration.
The host batch operation may be successful or unsuccessful. Obviously, the serial operation can not satisfy our demand. At this time, we need to introduce thread pool.
2.Use of thread pools to improve performance in this high concurrency scenario
Let’s start with a few important classes in the thread pool
1、newFixedThreadPoolCreate a thread pool that specifies the number of worker threads. Every time a task is submitted, a worker thread is created. If the number of worker threads reaches the maximum of the initial number of thread pools, the submitted tasks are stored in the pool queue.
2、newCachedThreadPoolCreate a cacheable thread pool. This type of thread pool is characterized by:
1).There is almost no limit to the number of worker threads created (in fact, there are limits, the number is Interger. MAX_VALUE), so that you can add threads to the thread pool flexibly.
2).If the task is not submitted to the thread pool for a long time, that is, if the worker thread is idle for a specified time (default is 1 minute), the worker thread will automatically terminate. After termination, if you submit a new task, the thread pool recreates a worker thread.
3、newSingleThreadExecutorCreate a single-threaded Executor, that is, create only one worker thread to perform tasks. If this thread ends abnormally, another thread will replace it and ensure sequential execution (which I think is its characteristic). The biggest feature of single worker threads is to ensure that tasks are executed sequentially, andAnd no more than one thread is active at any given time.
4、newScheduleThreadPoolCreate a fixed-length thread pool that supports both regular and periodic task execution, similar to Timer
New Fixed ThreadPool is the main way for the landlord to test the effect.
The first is the traditional for loop serial.
Processing 10,000 pieces of data, assuming that 10 ms is consumed for each service processing, the total time consumed is 107857 Ms.
Next, we use thread pool with size of 100. It takes 1100 MS to process 10,000 pieces of data. Is it amazing and nearly 10 times more efficient?
At this point, the main thread pool in the building deals with massive data, or the simple application in high concurrency is over….
Similar to this thread pool tool class, there are many forums, you can refer to ~~
If there are any mistakes, please correct them.