?? objectpool.html
字號:
<html>
<head>
<title>snaq.net: Custom Object Pooling</title>
<META NAME="ROBOTS" CONTENT="NOINDEX,NOFOLLOW">
<meta name="Author" content="Giles Winstanley">
<meta name="keywords" content="pool, pooling, object pool">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="#D0D0D0">
<div align="center">
<table border="0" width="85%"><tr><td>
<!-- Start main page here -->
<div align="center">
<h1>Custom Object Pooling</h1>
<p>Copyright © Giles Winstanley<br>
Last updated: 8th July 2004</p>
</div>
<hr size="1">
<h2>Why would I do it?</h2>
<p>In any application with a lot of re-use of objects, where the cost of creating those objects becomes disproportionately high, pooling those objects may well assist in providing more performance for the application.</p>
<p>Suppose you wanted to write a highly multi-threaded application, you could create your own ThreadPool class which allowed you to easily re-use individual threads. If you were writing a complex particle system engine for a graphical system, you might want to create a ParticlePool class to re-use individual particles and increase the overall speed. One of the most common examples is of pooling database connections. Creating the connections takes a long time compared to the often relatively quick and simple operations performed with those connections.</p>
<p>Bear in mind that an analysis of your application's requirements should be done to determine whether a pooling system would actually help performance significantly compared to other possible changes.</p>
<h2>Where do I start?</h2>
<p>The easiest place to start is by looking at the source code of the ObjectPool class which does the majority of the work. It is an abstract class, meaning that the class needs to be sub-classed and some of it's methods over-ridden to provide implementations. Shown below are the methods that require implementation by a sub-class:</p>
<pre>
<font color="teal">
/**
* Object creation method.
* This method is called when a new item needs to be created following a call
* to one of the check-out methods.
* @exception Exception if unable to create the item
*/</font>
protected abstract Reusable create() <font color="maroon">throws Exception</font>;
<font color="teal">
/**
* Object validation method.
* This method is called when checking-out an item to see if it is valid for use.
* When overridden by the sub-class it is recommended that this method perform
* suitable checks to ensure the object can be used without problems.
*/</font>
protected abstract boolean isValid(final Reusable o);
<font color="teal">
/**
* Object destruction method.
* This method is called when an object needs to be destroyed due to pool
* pruning/cleaning, or final release of the pool.
*/</font>
protected abstract void destroy(final Reusable o);
</pre>
<p>The other methods are not shown, but they provide support for the pool creation, object pooling, obtaining/returning ("checking-in" & "checking-out") objects, objects expiry, etc. What we need concern ourselves with is the actual creation, destruction, and validation of objects within the pool, which is taken care of by these methods.</p>
<p>Each of the abstract methods in ObjectPool make reference to a Reusable object. This is simply an object that adheres to the following specification:</p>
<pre>
<font color="teal">
/**
* Interface for an object that can be reused.
*/</font>
public interface Reusable
{<font color="teal">
/**
* Cleans an object to put it in a state in which it can be reused.
* @throws Exception if unable to recycle the this object
*/</font>
public void recycle() <font color="maroon">throws Exception</font>;
}
</pre>
<p>This interface is designed to specify objects that can be reused multiple times. The <code>recycle()</code> method is called on an object just before the object is placed back in the pool ready to be used again. Bear this in mind when implementing the method in your own object classes.</p>
<h2>What next?</h2>
<p>Now you know what has to be implemented it's time to think about how to achieve it. The objects you want to place in the pool must implement Reusable to clean them before being used again. This may be as simple as resetting a few variables to their initial state, or it may be more complex and require closing old network or database connections and establishing new ones. This is very important to get right as lack of care here could produce surprisingly dramatic resource leaks when it comes to repeated reuse of those objects.</p>
<p>Implementing the abstract methods of ObjectPool in your sub-class comes next. The <code>create()</code> method is responsible for creating new objects within the pool and should initialize each object so it is ready to be used. The <code>isValid()</code> method checks whether an object is valid for use. With simple objects this may be a very short implementation (or even a blank implementation in some cases), but it may also involve extensive checks to ensure that the object can be used without problems. The <code>destroy()</code> method is called to destroy an object from the pool when either not needed or it has been found invalid. This could require the freeing of resources that the object uses.</p>
<p>In addition it is recommended that you provide two other methods for obtaining and returning objects from the pool. These methods effectively provide an interface to the <code>checkIn()</code> and <code>checkOut()</code> of ObjectPool, but they can handle returned null values and cast the objects to the appropiate type before use.</p>
<p>That's it! It's a straightforward process to create a custom object pool, but often tricky details can become evident in the implentation. Take a closer look at the source code for the ConnectionPool class to see an an-depth example of how to do it. The basics remain the same, but there is a lot of other code to manage the details.</p>
<!-- End main page here -->
<hr size="1">
</font>
</td></tr></table>
</div>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -