1 16 package com.ibatis.common.util; 17 18 import com.ibatis.common.exception.NestedRuntimeException; 19 20 import java.util.List ; 21 import java.util.Collections ; 22 import java.util.ArrayList ; 23 24 27 public class ThrottledPool { 28 29 private Throttle throttle; 30 31 private Class type; 32 private List pool; 33 34 39 public ThrottledPool(Class type, int size) { 40 try { 41 this.throttle = new Throttle(size); 42 this.type = type; 43 this.pool = Collections.synchronizedList(new ArrayList (size)); 44 for (int i=0; i < size; i++) { 45 this.pool.add(type.newInstance()); 46 } 47 } catch (Exception e) { 48 throw new NestedRuntimeException("Error instantiating class. Cause: " + e, e); 49 } 50 } 51 52 56 public Object pop() { 57 Object o = null; 58 while (o == null) { 59 try { 60 throttle.increment(); 61 o = pool.remove(0); 62 } catch (Exception e) { 63 } 65 } 66 return o; 67 } 68 69 73 public void push(Object o) { 74 if (o != null && o.getClass() == type) pool.add(o); 75 throttle.decrement(); 76 } 77 78 } 79 | Popular Tags |