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

            常用鏈接

            統(tǒng)計

            積分與排名

            百事通

            最新評論

            對象池(套用了網上的一個有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

            久久99国产精品久久99小说| 日韩精品无码久久一区二区三| 无码任你躁久久久久久老妇App| 久久久久久精品免费免费自慰 | 93精91精品国产综合久久香蕉| 国产精品日韩深夜福利久久| 亚洲AV日韩AV天堂久久| 国产精品成人精品久久久| 国产A三级久久精品| 精品久久国产一区二区三区香蕉 | 成人精品一区二区久久久| 久久久无码精品亚洲日韩京东传媒 | 97热久久免费频精品99| 久久激情五月丁香伊人| 日本强好片久久久久久AAA| 国产精品久久久久9999高清| 久久午夜福利无码1000合集| 岛国搬运www久久| 青青草原综合久久大伊人精品| 亚洲中文久久精品无码ww16| 亚洲国产成人久久一区WWW| 久久综合欧美成人| 久久精品这里热有精品| 国产麻豆精品久久一二三| 久久99久久99精品免视看动漫| 性做久久久久久久久久久| 91精品久久久久久无码| 国产福利电影一区二区三区,免费久久久久久久精 | 久久国产免费直播| 久久国产精品-久久精品| av无码久久久久不卡免费网站| 久久精品国产亚洲AV忘忧草18| 国产香蕉久久精品综合网| 亚洲国产精品成人AV无码久久综合影院| 国产成人精品综合久久久| 国产L精品国产亚洲区久久| 国产精品VIDEOSSEX久久发布| 久久国产高清一区二区三区| 91超碰碰碰碰久久久久久综合| 国内精品免费久久影院| 精品久久人人做人人爽综合|