• <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

            伊人伊成久久人综合网777| 新狼窝色AV性久久久久久| 国产精品99久久不卡| 久久国产香蕉一区精品| 伊人久久大香线蕉亚洲五月天| 久久久婷婷五月亚洲97号色| 99久久国产热无码精品免费久久久久 | 久久精品成人欧美大片| 99久久国语露脸精品国产| 久久国产热这里只有精品| 国产成人精品三上悠亚久久| 久久精品国产精品青草| 亚洲愉拍99热成人精品热久久 | 欧美牲交A欧牲交aⅴ久久| 亚洲伊人久久大香线蕉苏妲己| 狠狠色丁香久久婷婷综合_中| 成人久久精品一区二区三区| 久久精品国产亚洲αv忘忧草| 一级做a爰片久久毛片人呢| 久久久久久亚洲AV无码专区| 欧美激情精品久久久久久久| 久久er国产精品免费观看2| 人妻精品久久无码区| 区久久AAA片69亚洲| 日本精品久久久久影院日本| 无码国内精品久久人妻蜜桃| 欧美va久久久噜噜噜久久| 久久久久亚洲av毛片大| 91精品免费久久久久久久久| 色妞色综合久久夜夜| 99久久国产宗和精品1上映| 色综合久久夜色精品国产| 久久精品国产72国产精福利| 91性高湖久久久久| 香蕉久久一区二区不卡无毒影院 | 久久精品二区| 久久综合色区| 狠狠色婷婷久久一区二区| 亚洲色欲久久久综合网东京热| 久久经典免费视频| 色婷婷综合久久久久中文一区二区|