ORG.APache.commons.pool --- Apache's object pool implementation.

xiaoxiao2021-03-06  61

Object pool technology is widely used in server development. In the implementation of various object pools, especially the connection pool of the database is most obvious, it can be said that each server must implement. This article is a record of the personal learning object pool, implemented as a research object with Apache's Commons-pool. In the next blog, I will continue to study Apache's CommON-DBCP, which is an application example of the object pool technology on JDBC. The implementation of the Apache object pool technology is quite simple, only 2 main objects: ObjectPool, used to manage all objects in the object pool, as well as the object's lending, and recycling. PoolableObjectFactory, generated, borrowed and recycled objects by ObjectPool. The reason is called Factory, just because it exports several functions responsible for life management: makeObject, DestoryObject, ActiveObject .... ObjectPool maintains a list where all generated objects are stored. Several methods are also derived, such as BorrowObject, ReturnObject, AddObject, and more. When the user calls BorrowObject, ObjectPool looks at the number of idle objects in the current list. If there is an idle object, then initialize the object to return to the user, otherwise you create an object and return to the user. Similarly, when the user calls ReturnObject, ObjectPool looks at the number of idle objects in the current queue. If the number is less than default_max_sleeping, the status of the object will be changed, and then in the queue, the alternate object is used; otherwise directly destroy the object. This alternative concept is the theoretical basis of the object pool, which can greatly reduce the number of times of object generation and destruction. For those who initialize the slower, reduce the number of object constructs and destructions is equal to a significant increase in overall efficiency. Especially for the database connection to the database, the application target pool technology is justified because the efficiency of JNDI search is extremely low. Apache's Commons-Pool is very simple, first need to create a number of objects you think there is a need for poolization, and implement several methods of PoolableOBejctFactory. Then construct an ObjectPool. Apache provides three ObjectPool: StackObjectPool, genericObjectPool, SoftReferenceOBejctPool, which is usually available in StackOBejctPool. Then when you need to create an object, use stackobejctpool.borrowobejct; After using the object, call StackObjectPool.ReturnObject, complete the operation of the object pool. It should be noted that the object pool technology is not applicable to any object. Because the operation of the target pool itself consumes some resources, for some small objects, the use of the object pool may achieve the opposite effect. There is a paper on IBM DeveloperWorks, which points to the simple object such as Point, Size, etc., using object pool technology, does not bring this improvement, and complex objects such as JPanel, JFrame, etc., which can bring a slight performance advantage after using the object pool; The most suitable object pool technology is some time-consuming operation, such as JDBC connection, thread, etc.

转载请注明原文地址:https://www.9cbs.com/read-92015.html

New Post(0)