Example usage for org.apache.commons.pool.impl SoftReferenceObjectPool SoftReferenceObjectPool

List of usage examples for org.apache.commons.pool.impl SoftReferenceObjectPool SoftReferenceObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl SoftReferenceObjectPool SoftReferenceObjectPool.

Prototype

public SoftReferenceObjectPool() 

Source Link

Usage

From source file:org.codelabor.system.remoting.tcp.factories.SocketPoolTest.java

/**
 * Test method for//from www. ja  va 2 s  .c  om
 * {@link org.codelabor.system.remoting.tcp.factories.SocketPoolFactory#makeObject()}
 * .
 * 
 * @throws Exception
 * @throws IllegalStateException
 * @throws NoSuchElementException
 */
@Test
public void testSoftReferenceObjectPool() throws Exception {
    SoftReferenceObjectPool socketPool = new SoftReferenceObjectPool();
    SocketPoolFactory socketPoolFactory = new SocketPoolFactory();
    socketPoolFactory.setHost("localhost");
    socketPoolFactory.setPort(8080);
    socketPool.setFactory(socketPoolFactory);
    Socket sockets[] = new Socket[10];

    for (int i = 0; i < 10; i++) {
        sockets[i] = (Socket) socketPool.borrowObject();
        System.out.println("borrowObject: " + sockets[i].hashCode());
        System.out.println("active: " + socketPool.getNumActive() + ", idle: " + socketPool.getNumIdle());

    }
    for (int i = 0; i < 10; i++) {
        System.out.println("returnbject: " + sockets[i].hashCode());
        socketPool.returnObject(sockets[i]);
        sockets[i] = null;
        System.out.println("active: " + socketPool.getNumActive() + ", idle: " + socketPool.getNumIdle());
    }
    // while (true) {
    // Thread.sleep(1000);
    // System.out.println("active: " + socketPool.getNumActive()
    // + ", idle: " + socketPool.getNumIdle());
    // }

}