美文网首页程序员源码学习
连接池commons-pool源码学习之Hello World

连接池commons-pool源码学习之Hello World

作者: zyzab | 来源:发表于2017-10-13 15:54 被阅读0次

缓存池的作用就是减少重复创建资源,不是到每次需要使用资源再创建,而是提前准备好资源,每次需要时,从池中获取,用完归还.

我们看看下面简单的 Hello World

从流中读取字符串,每次都需要创建StringBuffer

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 
            } 
        } 
    } 
}

有些麻烦吧,我们使用缓存池获取StringBuffer试试,直接上代码

import java.io.IOException;
import java.io.Reader;
import org.apache.commons.pool2.ObjectPool;

public class ReaderUtil {
    
    private ObjectPool<StringBuffer> pool;
    
    public ReaderUtil(ObjectPool<StringBuffer> 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 = 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
            }
        }
    }
}

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

public class StringBufferFactory
    extends BasePooledObjectFactory<StringBuffer> {

    //创建对象的方法
    @Override
    public StringBuffer create() {
        return new StringBuffer();
    }

    /**
     * Use the default PooledObject implementation.
     * 使用默认的池化对象DefaultPooledObject封装StringBuffer,使之带上状态属性
     */
    @Override
    public PooledObject<StringBuffer> wrap(StringBuffer buffer) {
        return new DefaultPooledObject<StringBuffer>(buffer);
    }

    /**
     * When an object is returned to the pool, clear the buffer.
     * 钝化对象,下次之前可以再复用该对象
     */
    @Override
    public void passivateObject(PooledObject<StringBuffer> pooledObject) {
        pooledObject.getObject().setLength(0);
    }

    // for all other methods, the no-op implementation
    // in BasePooledObjectFactory will suffice
}
ReaderUtil readerUtil = new ReaderUtil(new GenericObjectPool<StringBuffer>(new StringBufferFactory()));

代码很简单,下回代码源码解读.

相关文章

  • 连接池commons-pool源码学习之Hello World

    缓存池的作用就是减少重复创建资源,不是到每次需要使用资源再创建,而是提前准备好资源,每次需要时,从池中获取,用完归...

  • 连接池commons-pool源码学习

    上一篇简单的hello world 理解commons pool,需要了解主要类,接口 PooledObject ...

  • Spring Boot 学习笔记

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spr...

  • Kotlin基础之函数与变量

    开始学习走路之"Hello world!" 上述为分别使用Java和Kotlin语言编写的"Hello World...

  • 常用markdown语法

    Hello World! Hello World! Hello World! Hello World! Hello...

  • hello

    hello, world hello, world hello, world hello, world

  • Markdown

    标题: hello world hello world hello world hello world hello...

  • 2018-06-11

    markdown hello world hello world hello world hello world ...

  • Python学习之Hello World

    自从大一之后就一直使用java到现在,最近总觉得应该学习一下其它语言。看了一下python,据说用来做网络爬虫和机...

  • Hello World

    Hello World 1 Hello World 2## Hello World 3 Hello World 4...

网友评论

    本文标题:连接池commons-pool源码学习之Hello World

    本文链接:https://www.haomeiwen.com/subject/pdizyxtx.html