Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the java object pool

Shulou Source: shulou.com Published: 2022-05-31 10:48:38 09月18日 Update

This article mainly introduces "how to use java object pool". In daily operation, I believe many people have doubts about how to use java object pool. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use java object pool". Next, please follow the editor to study!

Description

1. Create a factory class, inherit or implement the basic interface.

By inheriting BaseGenericObjectPool or implementing the basic interface PooledObjectFactory, and rewriting the methods of creating, destroying, verifying, activating, and passivating objects according to business requirements, most of them are connection closing, empty, and so on.

2. Create a pool and inherit GenericObjectPool.

By inheriting GenericObjectPool or implementing the basic interface ObjectPool, it is recommended to use the former, which provides us with a mechanism for detecting the expulsion of idle objects (that is, destroying objects that have not been used for a long time in the idle queue to reduce memory footprint), as well as providing basic information about many objects, such as the last time the object was used, whether it is checked before using the object, and so on.

3. Create pool-related configurations

To add configuration control to the thread pool by inheriting GenericObjectPoolConfig or BaseObjectPoolConfig, it is recommended to use the former, which implements the basic method for us and only needs to add the attributes we need.

4. Create a wrapper class

That is, for the objects that exist in the object pool, many basic properties are added to the actual objects to make it easy to understand the real-time state of the objects in the object pool.

Example

Private volatile int maxIdle = GenericObjectPoolConfig.DEFAULT_MAX_IDLE;private volatile int minIdle = GenericObjectPoolConfig.DEFAULT_MIN_IDLE;public static final int DEFAULT_MAX_IDLE = 8 × public static final int DEFAULT_MIN_IDLE = 0; at this point, the study of "how to use the java object pool" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Objects basics learning interfaces methods properties suggestions time more free help configuration utility next business information memory instance real-time actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei Docker macOS Shulou Technology