/**
* 排版器的一个公用接口 <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();
}
}
}