⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyedobjectpoolsample.java

📁 这是一个关于J2EE的开源包common里的许多组件的示例应用程序,可以借鉴.
💻 JAVA
字号:
/**
 * Title : Base Dict Class
 * Description : here Description is the function of class, here maybe multirows    
 * @author        kevin
 * @Version       1.0 
 */

package pool;

/**
 * Class description goes here.
 * @version 1.0  2005-12-5 
 * @author kevin
 */
import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
import org.apache.commons.pool.KeyedObjectPool;
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.KeyedPoolableObjectFactory;
import org.apache.commons.pool.impl.StackKeyedObjectPoolFactory;

class KeyedPoolableObjectFactorySample
        extends BaseKeyedPoolableObjectFactory {

    public Object makeObject(Object key) throws Exception {
        return new String("[" + key + "]");
    }

}

public class KeyedObjectPoolSample {
    public static void main(String[] args) {
        Object obj = null;
        KeyedPoolableObjectFactory factory
                = new KeyedPoolableObjectFactorySample();
        KeyedObjectPoolFactory poolFactory 
                = new StackKeyedObjectPoolFactory(factory);
        KeyedObjectPool pool = poolFactory.createPool();
        String key = null;
        try {
            for (long i = 0; i < 10 ; i++) {
                 key = "" + (int) (Math.random() * 10);
                System.out.println("== " + i + " ==");
                System.out.println("Key:" + key);
                obj = pool.borrowObject(key);
                System.out.println("Object:" + obj);
                pool.returnObject(key, obj);
                obj = null;
            }
        }
        catch (Exception e) {
                 e.printStackTrace();
        }
        finally {
            try{
                if (obj != null) {
                    pool.returnObject(key, obj);
                }
                pool.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -