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(final PoolableObjectFactory factory, final int initSize) throws Exception 

Source Link

Usage

From source file:TestRedundantObjectPool.java

 public static void main(String args[]) throws Exception {

   SoftReferenceObjectPool pool =//from   www.  j  av a 2s. c  o m
     new SoftReferenceObjectPool(new EmployeeFactory(), 5);

   try{

      System.err.println("Number of employees in pool: " + pool.getNumIdle());

      Employee employee = (Employee)pool.borrowObject();

      System.err.println("Borrowed Employee: " + employee);

      employee.doWork();

      pool.returnObject(employee);

      // employee = null;

      HashMap map = new HashMap();

      System.err.println("Running memory intensive operation");
      for(int i = 0; i < 1000000; i++) {
         map.put(new Integer(i), new String("Fred Flintstone" + i));
      }

   }catch(OutOfMemoryError e) {
      System.err.println("Borrowed employee after OutOfMemory: " +
        pool.borrowObject());
      System.err.println("Error: "  + e);
   }
}

From source file:TestSoftRef.java

 public void setUp() throws Exception {
   this.pool = new SoftReferenceObjectPool(new PoolableObjectFactory() {
       int counter;
       public Object makeObject()                   { return String.valueOf(counter++); }
       public void destroyObject( Object obj )      {}
       public boolean validateObject( Object obj )  { return true; }
       public void activateObject( Object obj )     {}
       public void passivateObject( Object obj )    {}
   }, 5);// ww  w . j a  va2 s.co m
}