自分で書いたマルチスレッドオブジェクトプール


/**
 *            <br>
 *          ,               .<br>
 * 
 */
public interface ILayouter {

	/**
	 *   
	 */
	void layout();

	/**
	 *   
	 */
	void reset();

	/**
	 *   
	 */
	void dispose();
}
 
public class LeafLayouter implements ILayouter {

	private static AtomicInteger	ai	= new AtomicInteger(0);

	public LeafLayouter() {
		ai.incrementAndGet();
	}

	public void dispose() {

	}

	public void layout() {
		System.out.println(Thread.currentThread().getName() + ":"
				+ this.hashCode() + "#" + ai.get() + "#==>layout!");

	}

	public void reset() {
		System.out.println(Thread.currentThread().getName() + ":"
				+ this.hashCode() + "#" + ai.get() + "#==>reset!");

	}
}
 
class LayouterFactory {

	static <T extends ILayouter> T newInstance(Class<T> c) {
		try {
			return c.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}
}
 
/**
 *         <br>
 *                     ,         .<br>
 * 
 */
public final class LayouterPool {

	/**
	 *   
	 */
	private static Logger						_log;
	/**
	 *          
	 */
	private Map<Class<?>, LayoterHandler<?>>	poolCache;

	/**
	 *       .   .
	 */
	private LayouterPool() {
		this.poolCache = new HashMap<Class<?>, LayoterHandler<?>>();
	}

	/**
	 * safe and easy lazy init.
	 */
	private static class PoolHolder {
		private static final LayouterPool	pool	= new LayouterPool();
	}

	/**
	 *              
	 * 
	 * @return       
	 */
	public static LayouterPool getInstance() {
		_log = Logger.getLogger(LayouterPool.class.getName());
		_log.setLevel(Level.WARNING);
		return PoolHolder.pool;
	}

	/**
	 *          ,                .
	 * 
	 * @return      
	 */
	public LeafLayouter getLeafLayouter() {
		return this.getHandler(LeafLayouter.class, 10).get();
	}

	/**
	 *         
	 * 
	 * @param c
	 *                 
	 * @param maxCount
	 *                      
	 * @return       
	 */
	@SuppressWarnings("unchecked")
	private synchronized <T extends ILayouter> LayoterHandler<T> getHandler(
			Class<T> c, int maxCount) {//           
		LayoterHandler<T> handler = (LayoterHandler<T>) this.poolCache.get(c);
		if (handler == null) {
			// synchronized (this.poolCache) {//        
			handler = new LayoterHandler<T>(c, maxCount);
			this.poolCache.put(c, handler);
			// }
		}
		return handler;
	}

	/**
	 *        <br>
	 *                 .
	 * 
	 * @param layouter
	 *               
	 */
	@SuppressWarnings("unchecked")
	public void release(ILayouter layouter) {//     ,     
		if (layouter == null) {
			return;
		}
		LayoterHandler<ILayouter> handler = (LayoterHandler<ILayouter>) this.poolCache
				.get(layouter.getClass());
		if (handler != null) {
			handler.release(layouter);
		}
	}

	/**
	 *       <br>
	 *         ,        .<br>
	 *           ,                .
	 */
	private class LayoterHandler<T extends ILayouter> {
		/**
		 *     
		 */
		private int			capacity;
		/**
		 *       
		 */
		private Queue<T>	idlers;
		/**
		 *        
		 */
		private List<T>		rusher;
		/**
		 *     
		 */
		private Class<T>	clazz;
		/**
		 *         
		 */
		private Semaphore	semaphore;

		/**
		 *            .
		 * 
		 * @param c
		 *                 
		 * @param capacity
		 *                
		 */
		LayoterHandler(Class<T> c, int capacity) {
			this.clazz = c;
			this.capacity = capacity;
			this.idlers = new ArrayBlockingQueue<T>(this.capacity);
			this.rusher = new ArrayList<T>(this.capacity);
			this.semaphore = new Semaphore(this.capacity);
		}

		/**
		 *      .<br>
		 *                 ,          .<br>
		 *          ,              
		 * <ol>
		 * <li> :          </li>
		 * <li> :                ,        </li>
		 * </ol>
		 * 
		 * @return    
		 */
		T get() {
			try {
				this.semaphore.acquire();
			} catch (InterruptedException e) {
				_log.log(Level.SEVERE, e.getMessage(), e);
			}
			T t = this.idlers.poll();
			if (t == null) {
				t = LayouterFactory.newInstance(this.clazz);
			}
			this.rusher.add(t);
			return t;
		}

		/**
		 *        
		 * 
		 * @param t
		 *               
		 */
		void release(T t) {
			if (t == null) {
				return;
			}
			t.reset();
			if (this.idlers.offer(t)) {
				this.rusher.remove(t);
				this.semaphore.release();
			}
		}
	}
}
 
public class LayouterTest {

	public static void main(String[] args) {
		for (int i = 0; i < 200; i++) {
			new Thread() {

				@Override
				public void run() {
					LayouterPool instance = LayouterPool.getInstance();
					// System.out.println(instance);
					ILayouter layouter = instance.getLeafLayouter();
					layouter.layout();
					try {
						sleep(new Random().nextInt(5000));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					instance.release(layouter);
				}

			}.start();
		}
	}
}