Implementation of dedicated Object pool
Because the management overhead of the general object pool is relatively large, It offsets most of the advantages of reusing objects to some extent. To solve this problem, you can use the dedicated Object pool method. That is, the class type of the object pooled by the object pool is not dynamically specified, but has been specified in advance. In this way, it is easier to implement than the general object pool. Instead of the objectpoolfactory and poolableobjectfactory classes, they can directly integrate their functions into the objectpool class, the details are as follows (assuming that the class type of the pooled object is still stringbuffer, and the place indicated by the ellipsis indicates that the code is implemented in the same way as the general object pool ):
Public class objectpool {
Private parameterobject paraobj; // attribute parameter object of the Object pool
Private int currentnum = 0; // number of currently created objects in the object pool
Private stringbuffer currentobj; // objects that can be lent out by the object pool currently
Private vector pool; // The pool used to store objects
Public objectpool (parameterobject paraobj ){
This. paraobj = paraobj;
Pool = new vector ();
}
Public stringbuffer GetObject (){
If (pool. Size () <= paraobj. getmincount ()){
If (currentnum <= paraobj. getmaxcount ()){
Currentobj = new stringbuffer ();
Currentnum ++;
}
...
}
Return currentobj;
}
Public void returnobject (Object OBJ ){
// Ensure that the object has the correct type
If (stringbuffer. isinstance (OBJ )){
...
}
} |
Conclusion
The appropriate use of Object pool technology can effectively improve the performance of applications. Currently, Object pool technology has been widely used. For heavyweight objects such as network and database connections, Object pool technology is generally used. Note the following when using the object pool technology:
· Object pool technology is not applicable in any situation. Basically, the object pool technology is applicable only when the operation that repeatedly generates an object becomes a key factor affecting performance. However, it is not important to improve the performance caused by pooling. It is better not to use the object Pooling technology to keep the code concise.
· Correctly select the implementation method of the Object pool based on the actual situation. If you want to create a public object pool technical implementation package, or you need to dynamically specify the class type of the pooled object in the program, select the general object pool. In most cases, you can use a dedicated Object pool.