⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 examples.html

📁 优秀的文档,可以学习java之用 0006728337 00000 n 0006728424 00000 n 0006728600 00000 n
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>Examples - Commons Pool</title><style type="text/css" media="all">          @import url("./style/maven-base.css");                    @import url("./style/maven-theme.css");@import url("./style/project.css");</style><link rel="stylesheet" href="./style/print.css" type="text/css" media="print"></link><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></meta><meta name="author" content="Commons Documentation Team"></meta><meta name="email" content="commons-dev@jakarta.apache.org"></meta><meta name="author" content="Rodney Waldhoff"></meta><meta name="email" content="rwaldhoff@apache.org"></meta></head><body class="composite"><div id="banner"><a href="http://jakarta.apache.org" id="organizationLogo"><img alt="The Apache Software Foundation" src="http://jakarta.apache.org/images/original-jakarta-logo.gif"></img></a><a href="http://jakarta.apache.org/commons/pool/" id="projectLogo"><img alt="Commons Pool" src="./images/pool-logo-white.png"></img></a><div class="clear"><hr></hr></div></div><div id="breadcrumbs"><div class="xleft">Last published: 02 April 2006                <span class="separator">|</span>燚oc for  1.3                </div><div class="xright">                <a href="http://jakarta.apache.org/commons/" class="externalLink" title="External Link">Jakarta Commons</a>      </div><div class="clear"><hr></hr></div></div><div id="leftColumn"><div id="navcolumn"><div id="menuCommons燩ool"><h5>Commons燩ool</h5><ul><li class="none"><a href="index.html">Overview</a></li><li class="none"><a href="apidocs/index.html">Javadoc</a></li><li class="collapsed"><a href="guide/index.html">Developers燝uide</a></li><li class="none"><strong><a href="examples.html">Examples</a></strong></li><li class="none"><a href="downloads.html">Downloads</a></li><li class="none"><a href="http://wiki.apache.org/jakarta-commons/Pool" class="externalLink" title="External Link">Wiki</a></li></ul></div><div id="menuProject_Documentation"><h5>Project Documentation</h5><ul><li class="none"><a href="index.html">About</a></li><li class="collapsed"><a href="project-info.html">Project Info</a></li><li class="collapsed"><a href="maven-reports.html">Project Reports</a></li><li class="none"><a href="http://jakarta.apache.org/commons/charter.html" class="externalLink" title="External Link">Development Process</a></li></ul></div><div id="menuCommons"><h5>Commons</h5><ul><li class="none"><a href="http://jakarta.apache.org/commons/" class="externalLink" title="External Link">Home</a></li><li class="collapsed"><a href="http://jakarta.apache.org/commons/components.html" class="externalLink" title="External Link">Components</a></li><li class="collapsed"><a href="http://jakarta.apache.org/commons/sandbox/index.html" class="externalLink" title="External Link">Sandbox</a></li><li class="collapsed"><a href="http://jakarta.apache.org/commons/dormant/index.html" class="externalLink" title="External Link">Dormant</a></li><li class="none"><a href="http://jakarta.apache.org/commons/volunteering.html" class="externalLink" title="External Link">Volunteering</a></li><li class="none"><a href="http://jakarta.apache.org/commons/patches.html" class="externalLink" title="External Link">Contributing Patches</a></li><li class="none"><a href="http://jakarta.apache.org/commons/building.html" class="externalLink" title="External Link">Building Components</a></li><li class="none"><a href="http://jakarta.apache.org/commons/releases/index.html" class="externalLink" title="External Link">Releasing Components</a></li><li class="none"><a href="http://wiki.apache.org/jakarta-commons/FrontPage" class="externalLink" title="External Link">Wiki</a></li></ul></div><div id="legend"><h5>Legend</h5><ul><li class="externalLink">External Link</li><li class="newWindow">Opens in a new window</li></ul></div><a href="http://maven.apache.org/" title="Built by Maven" id="poweredBy"><img alt="Built by Maven" src="./images/logos/maven-feather.png"></img></a></div></div><div id="bodyColumn"><div class="contentBox"><div class="section"><a name="A_Simple_Pool_Client"></a><h2>A Simple Pool Client</h2>       <p>        Suppose you're writing a set of <code>java.io.Reader</code> utilities, and would like to        provide a method for dumping the contents of a <code>Reader</code> to a <code>String</code>.        Here's the code for the <code>ReaderUtil</code>, implemented without an <code>ObjectPool</code>:       </p>    <div class="source"><pre>import java.io.Reader; import java.io.IOException;  public class ReaderUtil {     public ReaderUtil() {     }      /**      * Dumps the contents of the {@link Reader} to a      * String, closing the {@link Reader} when done.      */     public String readToString(Reader in) throws IOException {         StringBuffer buf = new StringBuffer();         try {             for(int c = in.read(); c != -1; c = in.read()) {                 buf.append((char)c);             }             return buf.toString();         } catch(IOException e) {             throw e;         } finally {             try {                 in.close();             } catch(Exception e) {                 // ignored             }         }     } }</pre></div>         <p>        For the sake of this example, let's assume we want to to pool the <code>StringBuffer</code>s         used to buffer the <code>Reader</code>'s contents. (A pool of <code>StringBuffer</code>s         may or may not be useful in practice. We're just using it as a simple example here.)       </p>       <p>        Let's further assume that a complete pool implementation will be provided via         a constructor. (We'll show you how to create such an implementation in just a moment.)          Then to use the pool we simply call <code>borrowObject</code> to obtain the buffer, and        then call <code>returnObject</code> when we're done with it.        Then a <code>ReaderUtil</code> implementation using a pool of <code>StringBuffer</code>s might look         like this:       </p>    <div class="source"><pre>import org.apache.commons.pool.ObjectPool;import java.io.Reader; import java.io.IOException;  public class ReaderUtil {     private ObjectPool pool;     public ReaderUtil(ObjectPool pool) {         this.pool = pool;    }      /**      * Dumps the contents of the {@link Reader} to a      * String, closing the {@link Reader} when done.      */     public String readToString(Reader in) throws IOException {         StringBuffer buf = null;        try {             buf = (StringBuffer)(pool.borrowObject());            for(int c = in.read(); c != -1; c = in.read()) {                 buf.append((char)c);             }             return buf.toString();         } catch(IOException e) {             throw e;         } catch(Exception e) {            throw new RuntimeException("Unable to borrow buffer from pool" +                     e.toString());        } finally {             try {                 in.close();             } catch(Exception e) {                 // ignored             }             try {                if(null != buf) {                    pool.returnObject(buf);                }            } catch(Exception e) {                // ignored            }        }     } }</pre></div>        <p>       Since we've constrained ourselves to the <code>ObjectPool</code> interface, an arbitrary pool        implementation (returning, in our case, <code>StringBuffer</code>s) can be used.  When a different       or "better" pool implemenatation comes along, we can simply drop it into our <code>ReaderUtil</code>       without changing a line of code.        </p>      </div><div class="section"><a name="A_PoolableObjectFactory"></a><h2>A PoolableObjectFactory</h2>       <p>        Recall that Pool provides a simple toolkit for creating object pools.  The         <code>PoolableObjectFactory</code> interface is an important part of this toolkit.        <code>PoolableObjectFactory</code> defines lifecycle methods for pooled objects.         We can use it to separate the kinds of objects that are pooled and how they are         created, persisted, or destroyed, from the pooling algorithm itself.       </p>       <p>        Suppose we have an <code>ObjectPool</code> implementation that accepts a         <code>PoolableObjectFactory</code> (for example, any of the implementations in the        <code>org.apache.commons.pool.impl</code> package).  Then we need only provide         the factory implemenation in order to pool a new kind of object.         </p>       <p>        Here's a <code>PoolableObjectFactory</code> implementation that creates        <code>StringBuffer</code>s as used above.       </p>    <div class="source"><pre>import org.apache.commons.pool.BasePoolableObjectFactory;  public class StringBufferFactory extends BasePoolableObjectFactory {     // for makeObject we'll simply return a new buffer     public Object makeObject() {         return new StringBuffer();     }          // when an object is returned to the pool,      // we'll clear it out     public void passivateObject(Object obj) {         StringBuffer buf = (StringBuffer)obj;         buf.setLength(0);     }          // for all other methods, the no-op      // implementation in BasePoolableObjectFactory     // will suffice }</pre></div>        <p>       We can, for example, use this factory with the <code>StackObjectPool</code> to instantiate our       <code>ReaderUtil</code> as follows:      </p>    <div class="source"><pre>new ReaderUtil(new StackObjectPool(new StringBufferFactory()))</pre></div>        </div></div></div><div class="clear"><hr></hr></div><div id="footer"><div class="xright">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -