@@ -0,0 +1,2494 @@
+package com.eblly.objectPoolPattern;%0A%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0A/**%0A * object pool Pattern%0A * %E6%83%85%E6%99%AF%EF%BC%9A%E9%9C%80%E8%A6%81%E5%B0%87%E8%B3%87%E6%BA%90%E5%9B%9E%E6%94%B6%EF%BC%8C%E7%AD%89%E5%BE%85%E4%B8%8B%E6%AC%A1%E4%BD%BF%E7%94%A8%E8%80%8C%E4%B8%8D%E7%94%A8%E5%89%B5%E5%BB%BA%E5%B0%8D%E8%B1%A1%E3%80%82%0A *%0A * @author eblly%0A * @version 1.0%0A * @date Aug 12, 2015%0A **/%0Aabstract class ObjectPool%3CT%3E %7B%0A%0A    private Set%3CT%3E available = new HashSet%3C%3E();%0A    private Set%3CT%3E inuse = new HashSet%3C%3E();%0A%0A    abstract T create();%0A%0A    public synchronized T checkOut() %7B%0A        if (available.size() %3C= 0) %7B%0A            available.add(create());%0A        %7D%0A        T instance = available.iterator().next();%0A%0A        available.remove(instance);%0A        inuse.add(instance);%0A%0A        return instance;%0A    %7D%0A%0A    public synchronized void checkIn(T instance) %7B%0A        inuse.remove(instance);%0A        available.add(instance);%0A    %7D%0A%0A    public String toString() %7B%0A        return String.format(%22Pool available=%25d inUse=%25d%22, available.size(), inuse.size());%0A    %7D%0A%7D%0A%0A/**%0A * session%E5%AF%A6%E9%AB%94%0A */%0Aclass Session %7B%0A    private static int count = 1;%0A    private final int id;%0A%0A    public Session() %7B%0A        id = count++;%0A    %7D%0A%0A    public int getId() %7B%0A        return this.id;%0A    %7D%0A%0A    public String toString() %7B%0A        return (%22session id = %22 + this.id);%0A    %7D%0A%0A%7D%0A%0Aclass SessionPool extends ObjectPool%3CSession%3E %7B%0A%0A    @Override%0A    Session create() %7B%0A        return new Session();%0A    %7D%0A%0A%7D%0A%0Apublic class Main %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        SessionPool pool = new SessionPool();%0A        Session session = pool.checkOut();%0A        System.out.println(session.toString());%0A        System.out.println(pool.toString());%0A%0A        System.out.println();%0A        SessionPool pool2 = new SessionPool();%0A        Session session2 = pool2.checkOut();%0A        System.out.println(session2.toString());%0A        System.out.println(pool2.toString());%0A%0A        System.out.println();%0A        SessionPool pool3 = new SessionPool();%0A        Session session3 = pool3.checkOut();%0A        System.out.println(session3.toString());%0A        System.out.println(pool3.toString());%0A%0A        System.out.println();%0A        System.out.println(%22Checking in %22 + session2);%0A        pool.checkIn(session2);%0A        System.out.println(%22Checking in %22 + session3);%0A        pool.checkIn(session3);%0A        System.out.println(pool);%0A%0A        System.out.println();%0A        Session session4 = pool.checkOut();%0A        System.out.println(%22Checked out %22 + session4);%0A        Session session5 = pool.checkOut();%0A        System.out.println(%22Checked out %22 + session5);%0A        System.out.println(pool);%0A    %7D%0A%7D

add object pool pattern