아파 치 Commons - pool 2

6487 단어 자바
Apache 산하 대상 풀 프레임 워 크 common - pol 2 공식 사이트:https://commons.apache.org/proper/commons-pool/
설정 설명:
maxTotal             ,    -1,-1int  )

blockWhenExhausted     true ,      ,          

maxWaitMillis           ,    。  blockWhenExhausted     true  ,    。 -1        ,            。(long  )

testOnBorrow      falsetrue ,    factory.validateObject()   

testOnCreate       falsetrue ,    factory.validateObject()   

(  :   testOnBorrow    testOnCreate           true  ,     factory.validateObject() )

lifo           ,    truetruefalse           

fairness                          java.util.concurrent.locks.ReentrantLock.ReentrantLock       。     falsetruefalse       ,

timeBetweenEvictionRunsMillis             ,    。    -1 ,-1            。(long  )

evictionPolicyClassName       ,      org.apache.commons.pool2.impl.DefaultEvictionPolicy(String  )

minEvictableIdleTimeMillis         ,      1800000,     ,(long   )

softMinEvictableIdleTimeMillis          ,     -1 ,     ,(long   )

(  ,      ,        ,    )

maxIdle         ,    8int  )

minIdle         ,    0int   )

testWhileIdle      falsetruefalse ,     factory.activateObject() factory.validateObject()

testOnReturn     falsetrue  ,            ,        ,   factory.validateObject()  ,    ,    factory.destroyObject()  

numTestsPerEvictionRun                ,       。    3, (int  )。

  :

     0 ,     。

      0 ,          (int)Math.ceil(          / Math.abs(numTestsPerEvictionRun) );

      0 ,          Math.min( numTestsPerEvictionRun,          );

사용 사례:
대상 연못 공장 류 는 생산, 회수, 검사 대상 을 책임 진다.
public class StringBufferFactory
    extends BasePooledObjectFactory {

    @Override
    public StringBuffer create() {
        return new StringBuffer();
    }

    /**
     * Use the default PooledObject implementation.
     */
    @Override
    public PooledObject wrap(StringBuffer buffer) {
        return new DefaultPooledObject(buffer);
    }

    /**
     * When an object is returned to the pool, clear the buffer.
     */
    @Override
    public void passivateObject(PooledObject pooledObject) {
        pooledObject.getObject().setLength(0);
    }

    @Override
    public void activateObject(PooledObject p) throws Exception {
        // TODO Auto-generated method stub
        super.activateObject(p);
    }
}

구체 적 인 사용:
import org.apache.commons.pool2.ObjectPool;

public class ReaderUtil {

    private ObjectPool pool;

    public ReaderUtil(ObjectPool pool) {
        this.pool = pool;
    }

    public String readToString(String abc){
        StringBuffer buf;
        try {
            buf=pool.borrowObject();
            buf.append(abc);
        } catch (Exception e) {
            throw new RuntimeException("Unable to borrow buffer from pool" + e.toString());

        }

        String result=buf.toString().trim();
        try {
//System.out.println("pool sizebefore:"+pool.getNumActive());
System.out.println("pool size before:"+pool.getNumIdle());
            pool.returnObject(buf);
System.out.println("pool size before:"+pool.getNumIdle());
//System.out.println("pool size after:"+pool.getNumActive());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
}

원본 코드 다운로드
이 노트 만!

좋은 웹페이지 즐겨찾기