“This article has participated in the weekend learning plan, click to see details”

preface

Pooling is a very common programming technique that can significantly optimize application performance and reduce the resource overhead of frequently creating links on the system when the number of concurrent requests is high.

Common database connection pool, thread pool, pool, etc., the characteristics of them are will be able to reuse things in a particular pool maintenance, the minimum number of connections, the maximum number of connections, such as blocking queue configuration, convenient for unified management and reuse, usually live with some agent mechanism, compulsory recycling, monitoring of form a complete set of functions.


The thread pool

ThreadPoolExecutor, introduced in JDK 1.5, is a thread pool implementation with two important parameters: corePoolSize and maximumPoolSize, which control the execution of the thread pool.

It works as follows:

  • throughexecute()When submitting a task, the number of threads in the thread pool is less thancorePoolSize, the newly submitted task will be executed by creating a new thread, even if there are idle threads in the thread pool.
  • throughexecute()Submits a task when the number of threads in the thread pool reachescorePoolSizeWhen, the newly submitted task is put intoworkQueueWaits for threads in the thread pool to schedule execution.
  • throughexecute()When submitting a task, whenworkQueueThe storage is full andmaximumPoolSizeIs greater thancorePoolSize, the newly submitted task will be executed by creating a new thread.
  • When a thread in the thread pool is idle after completing a task, it attempts to run theworkQueueTo execute the task.
  • throughexecute()Submit the task when the number of threads in the thread pool reachesmaxmumPoolSizeAnd,workQueueAlso full when newly submitted tasks are filled byRejectedExecutionHandlerPerform the reject operation.
  • When the number of threads in the thread pool exceedscorePoolSizeAnd is not configuredallowCoreThreadTimeOut=true, idle time exceedskeepAliveTimeIs destroyed, keeping the number of threads in the thread pool equalcorePoolSize
  • When settingallowCoreThreadTimeOut=trueWhen any idle time exceedskeepAliveTimeAll threads are destroyed.

Dynamic thread pool

Take a look at this Java thread pool implementation principle and its practice in Meituan business, you will be very inspired.


Database connection pool

The default Spring database connection pool HikariCP is used as an example:

  • maximum-pool-size: The maximum number of connections in the connection pool (both idle and active) the default value is 10.
  • minimum-idle: The minimum number of free connections in the pool. The default value is 10, which is smaller than the maximum number of connections in the pool.
  • pool-name: The name of the connection pool.
  • auto-commit: Whether to automatically commit connections returned from the pool.
  • idle-timeout: Free time.
  • max-lifetime: Indicates the maximum lifetime of a connection in the connection pool.
  • connection-timeout: Connection timeout period.

Connection policy:

  • Less thanminimum-idle: If the current number of connections is less than the minimum number of connections, a new connection is created to process the database request. If there are idle connections in the connection pool, reuse idle connections.
  • Is greater thanminimum-idleAnd less thanmaximum-pool-size: If there are no connections in the free pool and the current number of connections is less than the maximum number, a new connection is created to process the request;
  • Greater than or equal tomaximum-pool-size: If the current number of connections is greater than or equal to the maximum number of connections, wait for the old connections to become available according to the time set in the configuration.
  • If you wait longer than this, an error is thrown to the user.

Object pooling

Normally, we would simply create an object, but this is not a good method for large objects or objects that take a lot of time to construct. Therefore, we can use object pools to avoid the problems mentioned above.

In order to avoid duplicate wheels, Apache Commons-pool is used to implement ObjectPool. The main methods are as follows:

  • borrowObject(): Method of getting objects from the object pool.
  • returnObject(T obj): Returns the object to the object pool.
  • invalidateObject(T obj): invalidates the object.
  • addObject(): Adds an object to the object pool.
  • getNumIdle(): Gets the number of objects currently idle in the object pool, that is, the number of objects not taken away for use.
  • getNumActive(): Gets the number of objects already in use, that is, the number of objects removed from the object pool by the user.
  • clear(): Clears all idle objects in the object pool.
  • close(): Closes the object pool.

PooledObject abstracts some properties that objects in an object pool should have. Note that this object is not really the object we want to store, but a layer of encapsulated objects, so the objects that are actually stored in the object pool are the encapsulated objects, the PooledObject object.


conclusion

The dynamic thread pool above made me wonder if bytecode enhancement techniques could be used to implicitly enhance the functionality of the thread pool without any feeling from the client.


Personal note

The contents of this blog are all notes made by the author to learn, invade and delete! If used for other purposes, please specify the source!