Turn: Principle and Implementation of Java object pool technology 3

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.