• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            misschuer

            常用鏈接

            統計

            積分與排名

            百事通

            最新評論

            對象池(套用了網上的一個有BUG代碼 自己補充的 沒實際用過)

            import java.util.Enumeration;
            import java.util.Vector;

            public class ObjectPool {

                private static final boolean PRINTABLE = true;
                private Class<?> clazz = null;
                private int numObjects = 10;
                private int maxObjects = 50;
                private Vector<PooledObject> objects = null;
                
                public static void main(String[] args) throws Exception {

                    ObjectPool pool = new ObjectPool(XX.class);
                    pool.createPool();
                    
                    XX xx = (XX) pool.getObject();
                    xx.reset();
                    xx.setAge(1);
                    xx.setSex("M");
                    
                    pool.returnObject(xx);
                    pool.closeObjectPool();
                }
                
                
                public ObjectPool(Class<?> clazz) {
                    
                    this.clazz = clazz;
                }
                
                public synchronized void createPool() throws Exception {
                    
                    if (this.objects != null)
                        return;
                    
                    this.objects = new Vector<PooledObject>();
                    for (int i = 0; i < this.numObjects; ++ i) {
                        
                        createObjects();
                    }
                }
                
                
                public synchronized Object getObject() throws Exception {
                    
                    if (this.objects == null)
                        return null;
                    
                    Object conn = this.getFreeObject();
                    
                    while (conn == null) {
                        wait(250);
                        conn = this.getFreeObject();
                    }
                    
                    return conn;
                }
                
                
                private Object getFreeObject() throws Exception {
                    
                    Object obj = this.findFreeObject();
                    
                    if (obj == null) {
                        
                        this.createObjects();
                        obj = this.findFreeObject();
                        if (obj == null) {
                            
                            return null;
                        }
                    }
                    
                    return obj;
                }
                
                private void createObjects() throws Exception {
                    
                    if (this.objects.size() < this.maxObjects) {
                        
                        this.message("created" + this.objects.size());
                        Object obj = this.clazz.newInstance();
                        this.objects.addElement(new PooledObject(this.objects.size(), obj));
                    }
                }
                
                
                private Object findFreeObject() {
                    
                    this.message("before free:--------");
                    this.print();
                    Object obj = null;
                    PooledObject pooledObject = null;
                    Enumeration<PooledObject> enumerate = objects.elements();
                    while (enumerate.hasMoreElements()) {
                        
                        pooledObject = (PooledObject) enumerate.nextElement();
                        if (!pooledObject.isBusy()) {
                            
                            pooledObject.setBusy(true);
                            obj = pooledObject.getObject();
                            this.message("free object");
                            break;
                        }
                    }
                    
                    this.message("after free:--------");
                    this.print();
                    
                    return obj;
                }
                
                
                public void returnObject(Object obj) {
                    
                    if (this.objects == null)
                        return;
                    
                    this.message("before return:--------");
                    this.print();
                    PooledObject pooledObject = null;
                    Enumeration<PooledObject> enumerate = objects.elements();
                    while (enumerate.hasMoreElements()) {
                        
                        pooledObject = (PooledObject) enumerate.nextElement();
                        if (obj == pooledObject.getObject()) {
                            
                            pooledObject.setBusy(false);
                            this.message("return object");
                            break;
                        }
                    }
                    this.message("after return:--------");
                    this.print();
                }
                
                
                public synchronized void closeObjectPool() {
                    
                    if (this.objects == null)
                        return;
                    
                    PooledObject pooledObject = null;
                    Enumeration<PooledObject> enumerate = objects.elements();
                    while (enumerate.hasMoreElements()) {
                        
                        pooledObject = (PooledObject) enumerate.nextElement();
                        this.message(pooledObject.toString());
                        if (pooledObject.isBusy()) {
                            this.message("wait 5000");
                            wait(5000);
                        }
                    }

                    this.objects.clear();
                    this.objects = null;
                }
                
                
                private void wait(int millionSeconds) {
                    
                    try {
                        Thread.sleep(millionSeconds);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                
                
                public void print() {
                    
                    Enumeration<PooledObject> enumerate = objects.elements();
                    while (enumerate.hasMoreElements()) {
                        
                        PooledObject pooledObject = (PooledObject) enumerate.nextElement();
                        this.message(pooledObject.toString());
                    }
                    
                    this.message("print end==============================");
                }
                
                
                public void message(String msg) {
                    
                    if (PRINTABLE) {
                        System.out.println(msg);
                    }
                }
                
                
                class PooledObject {     
                    
                    private int index = 0;
                    Object objection = null;// 對象     
                    boolean busy = false// 此對象是否正在使用的標志,默認沒有正在使用     

                    
            // 構造函數,根據一個 Object 構告一個 PooledObject 對象     
                    public PooledObject(int index, Object objection) {     

                        this.index = index;
                        this.objection = objection;
                        this.busy = false;
                    }     

                    // 返回此對象中的對象     
                    public Object getObject() {     
                        return objection;     
                    }     

                    // 設置此對象的,對象     
                    public void setObject(Object objection) {     
                        this.objection = objection;     

                    }     

                    // 獲得對象對象是否忙     
                    public boolean isBusy() {     
                        return busy;     
                    }     

                    // 設置對象的對象正在忙     
                    public void setBusy(boolean busy) {     
                        this.busy = busy;     
                    }

                    public int getIndex() {
                        return index;
                    }

                    public void setIndex(int index) {
                        this.index = index;
                    }
                    
                    @Override
                    public String toString() {
                        
                        return String.format("[PooledObject] index:%d, busy:%b", this.index, this.busy);
                    }
                } 
            }


            class XX {
                
                private int age = 0;
                private String sex = "";
                
                public XX() {
                    
                }
                
                public void reset() {
                    
                    this.age = 0;
                    this.sex = "";
                }

                public int getAge() {
                    return age;
                }

                public void setAge(int age) {
                    this.age = age;
                }

                public String getSex() {
                    return sex;
                }

                public void setSex(String sex) {
                    this.sex = sex;
                }
            }

            posted on 2015-05-18 18:49 此最相思 閱讀(201) 評論(0)  編輯 收藏 引用 所屬分類: Java

            久久精品国产免费观看| 久久午夜综合久久| 亚洲一区中文字幕久久| 日韩欧美亚洲综合久久影院Ds | 久久AV高清无码| 99热热久久这里只有精品68| 久久这里只有精品首页| 国产成人综合久久精品尤物| 国产亚洲美女精品久久久2020| 91精品国产综合久久四虎久久无码一级| 久久久精品国产亚洲成人满18免费网站| 99久久国产亚洲综合精品| 色综合久久最新中文字幕| 久久精品国产99久久久古代| 久久久国产精品| 99久久国产主播综合精品| 精品久久久久久中文字幕人妻最新| 内射无码专区久久亚洲| 久久美女网站免费| 久久天堂AV综合合色蜜桃网| 狠狠色丁香久久婷婷综合蜜芽五月| 曰曰摸天天摸人人看久久久| 91精品国产综合久久精品| 精产国品久久一二三产区区别 | 久久精品人人做人人妻人人玩| 日韩电影久久久被窝网| 久久成人精品| 久久高潮一级毛片免费| 久久国产精品偷99| 久久AAAA片一区二区| 久久精品国产精品亜洲毛片| 亚洲国产精品婷婷久久| 国产亚洲精午夜久久久久久 | 亚州日韩精品专区久久久| 精品水蜜桃久久久久久久| 99久久久久| 无码8090精品久久一区| 久久无码中文字幕东京热| 久久www免费人成看片| 久久久精品国产sm调教网站| 国产精品久久国产精麻豆99网站|