In the Java language, there are two ways to improve the execution efficiency of a program, one is to use threads, the other is to use thread pools. In a production environment, we usually do the latter. Why is that? Today we are going to talk about the benefits of thread pooling, and pooling techniques and their applications.

1. Pooling technology

Pooling is the practice of preparing resources in advance so that they can be reused as needed. There are two main advantages to pooling: advance preparation and reuse. Taking object creation in the Java language as an example, you go through the following steps to create an object:

  1. Finds symbolic references to classes in the constant pool based on the argument after the new identifier;
  2. If no symbol application is found (the class is not loaded), load, parse, initialize the class, etc.
  3. The VM allocates memory in the heap for the object, initializes the allocated memory to 0, and establishes the corresponding description structure for the object header (time-consuming operations: need to find the free area in the heap, modify the memory allocation status, etc.).
  4. Call the initialization method of the object (time-consuming operations: complicated logical verification operations of the user, such as I/O and numerical calculation, etc.).

As you can see from the above process, creating a class requires complex and time-consuming operations, so you should reuse existing classes as much as possible to ensure efficient running of the program. Of course, it is better to create these classes in advance, and these functions are implemented by pooling technology.

2. Application of pooling technology

Common applications of pooling techniques include thread pooling, memory pooling, database connection pooling, and HttpClient connection pooling.

2.1 the thread pool

The principle of a thread pool is simple, similar to the concept of a buffer in an operating system. A thread pool starts with a number of threads that are asleep. When the client has a new request, it wakes up a sleeping thread in the thread pool to handle the request from the client. When the request is processed, the thread is asleep again. Thread pooling can greatly improve application performance. Such as a provincial data of county bank network center, the peak concurrent client requests per second more than 100, if you create a new thread for each client request, the amount of CPU time and memory is very breathtaking, if using a thread pool with 200 threads, it will save a lot of system resources, Allowing more CPU time and memory to be used for actual business applications rather than frequent thread creation and destruction.

2.2 memory pool

How to better manage application memory usage and increase the frequency of memory usage is a question worth pondering for every developer. Memory pools provide a viable solution. During the creation of a memory pool, the system allocates enough memory resources to form a preliminary memory pool. Then each time the user requests memory, a chunk of free memory from the pool is returned and marked as used. When memory is freed, it is not actually called free or DELETE. Instead, it is put back into the memory pool with flags set to free. Finally, the pool is destroyed at the end of the application, freeing each chunk of memory in the pool. Advantages of memory pools:

  • Reducing memory fragmentation can be seen from the process of creating a memory pool. When creating a memory pool, regular memory blocks are allocated to reduce the generation of memory fragmentation.
  • Increased memory usage. This can be seen in the process of allocating and freeing memory. Instead of calling system-provided functions or operators to manipulate actual memory, each allocation and freeing is done by overusing memory in the pool.

Disadvantages of a memory pool: It can be a waste of memory, because using a memory pool requires a large chunk of unused memory to be allocated initially, and not all of it is used.

2.3 Database Connection Pool

The basic idea of database connection pool is to store database connections as objects in memory during system initialization. When users need to access the database, they do not establish a new connection, but take out an established idle connection object from the connection pool. Instead of closing the connection, the user puts the connection back into the pool for the next request, and the connection is managed by the pool itself. In addition, you can set connection pool parameters to control the initial number of connections in the connection pool, the upper and lower limits of connections, the maximum number of connections, and the maximum idle time of each connection. Of course, you can also monitor the number of connections, usage, and so on through the connection pool’s own management mechanism.

2.4 HttpClient Connection Pool

HttpClient is often used for HTTP service access. In our project, there will be a function to retrieve the execution status of the task using HttpClient. The request will be made once a second, and a Conection Reset exception will occur frequently. The problem is that each request to HttpClient creates a new connection. When connections are created more frequently than connections are closed, a large number of TIME_CLOSED connections are created in the system.

3. Introduction to thread pools

Thread pool is a mode used by threads. It separates the concepts of threads and tasks, uses threads to perform tasks, and provides a unified implementation method of thread management and task management, avoiding the performance cost caused by frequent creation and destruction of threads.

4. Analysis of the advantages of thread pool

A thread pool, by contrast, does not require frequent creation and destruction of threads. Once a thread is created, it stays in the pool by default until a task arrives and then uses the existing thread to execute the task, as shown in the following figure:

Advantage 1: Multiple threads reduce resource consumption

When a thread is created, the memory space of the private thread such as the virtual machine stack, local method stack, and program counter should be allocated, and when it is destroyed, the private space resources should be reclaimed, as shown in the following figure:A thread pool creates a thread and then puts it in a thread pool, so the first advantage of a thread pool over a thread isCan reuse threads, reduce the consumption of system resources.

Advantage 2: Improved response speed

Thread pools reuse existing threads to perform tasks, and threads are created when there are tasks, so thread pools can respond to and execute tasks faster than threads.

Advantage 3: Controls the number of threads and tasks

The thread pool provides more management functions, which are mainly reflected in the following two aspects:

  1. Control maximum concurrency: Thread pools can create a fixed number of threads, avoiding the problem of creating unlimited threads. When too much thread creation, which may lead to perform system slow, because the number of CPU cores is certain, can handle the number of tasks at the same time also is certain, and the practical long thread can cause thread thread malicious competition and frequent switching problem, leading to slow the program execution, so the appropriate number of threads is the key to high performance operation.
  2. Control the maximum number of tasks: if the number of tasks is infinite, and the memory is insufficient, it will lead to program execution error, and the thread pool can control the maximum number of tasks, when the number of tasks exceeds a certain number, it will use the rejection strategy to deal with the extra tasks, so as to ensure the healthy operation of the system.

Benefit 4: More enhancements

Thread pools provide more functionality than threads, such as timed execution and periodic execution.

conclusion

Pooling is the practice of preparing resources in advance so that they can be reused as needed. There are two main advantages to pooling: advance preparation and reuse. Thread pool is a typical scenario of pooling technology. The main advantages of thread pool are: 1. Reuse thread, reduce resource consumption; 2. Improve response speed; 3. Provides the ability to manage the number of threads and tasks; More enhancements.

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public number: Java interview analysis

Interview collection: gitee.com/mydb/interv…