1 8 9 15 package jfun.yan.factory; 16 17 26 public class ThreadLocalScope<T> implements Pool<T> { 27 private static final class ThreadLocalCache<T> extends ThreadLocal { 28 protected Object initialValue() { 29 return new CachingPool<T>(); 30 } 31 CachingPool<T> getPool(){ 32 return (CachingPool<T>)this.get(); 33 } 34 } 35 private transient ThreadLocalCache<T> cache = new ThreadLocalCache(); 36 private void readObject(java.io.ObjectInputStream in) 37 throws ClassNotFoundException , java.io.IOException { 38 in.defaultReadObject(); 39 cache = new ThreadLocalCache<T>(); 40 } 41 42 public T getInstance(Factory<T> factory) { 43 return cache.getPool().getInstance(factory); 44 50 } 51 public T getPooledInstance(T def) { 52 return cache.getPool().getPooledInstance(def); 53 } 54 public boolean isPooled(){ 55 return cache.getPool().isPooled(); 56 } 57 } 58 | Popular Tags |