1. 程式人生 > >對象池(Object Pool)

對象池(Object Pool)

gen 實現 分享 池技術 long generated boolean generate 添加狀態

1、對象池技術並沒有限制說只能創建一個對象,而且這種技術同樣適用於創建固定數量的對象,然而,這種情況下,你就得面對如何共享對象池裏的對象這種問題。

當創建多個對象會的代價會很大的時候,可以考慮使用對象池技術,目前已有的技術比如:線程池技術、數據庫連接池技術

2、UML圖(astah/jude)下載地址:

技術分享圖片

3、模擬一個數據庫連接池進行實現:

實現的接口:

 1 package com.xinye.test.pool;
 2 /**
 3  * 用戶需要的實際的東西都實現這個接口
 4  * @author xinye
 5  *
 6  */
 7 public interface IConnection {
 8     Object get();
 9     void set(Object obj);
10 }

實現類:

 1 package com.xinye.test.pool;
 2 /**
 3  * 用戶真正需要的東西,比如數據庫連接
 4  * @author xinye
 5  *
 6  */
 7 public class Connection implements IConnection{
 8 
 9     @Override
10     public Object get() {
11         // TODO Auto-generated method stub
12         return null;
13     }
14 
15     @Override
16     public void set(Object obj) {
17         // TODO Auto-generated method stub
18         
19     }
20 
21 }

實現類的包裝對象(添加狀態):

 1 package com.xinye.test.pool;
 2 /**
 3  * 池子中放的東西(具有狀態以及用戶實際需要的東西,實際上就是個包裝類)
 4  * @author xinye
 5  *
 6  */
 7 public class PoolItem {
 8     public boolean isUse;
 9     public IConnection conn;
10     public PoolItem(IConnection conn){
11         this.conn = conn;
12     }
13 }

池子管理對象:

 1 package com.xinye.test.pool;
 2 
 3 import java.util.ArrayList;
 4 /**
 5  * 池子管理類
 6  * @author wangheng
 7  *
 8  */
 9 public class PoolManager {
10     
11     private ArrayList<PoolItem> items = new ArrayList<PoolItem>();
12     /**
13      * 往池子裏面放東西
14      * @param conn
15      */
16     public synchronized void add(IConnection conn){
17         items.add(new PoolItem(conn));
18     }
19     /**
20      * 得到池子中的對象
21      * @return
22      * @throws PoolEmptyException
23      */
24     public synchronized IConnection get() throws PoolEmptyException{
25         int len = items.size();
26         for(int i = 0;i < len;i++){
27             PoolItem item = items.get(i);
28             if(item.isUse == false){
29                 item.isUse = true;
30                 return item.conn;
31             }
32         }
33         throw new PoolEmptyException();
34     }
35     /**
36      * 釋放對象
37      * @param conn
38      * @throws PoolEmptyException
39      */
40     public synchronized void release(IConnection conn) throws PoolEmptyException{
41         int len = items.size();
42         for(int i = 0;i < len;i++){
43             PoolItem item = items.get(i);
44             if(conn == item.conn){
45                 item.isUse = false;
46                 return;
47             }
48         }
49         throw new PoolEmptyException();
50     }
51     /**
52      * 池子是空的異常
53      * @author wangheng
54      *
55      */
56     public static class PoolEmptyException extends Exception{
57         /**
58          * 
59          */
60         private static final long serialVersionUID = 5617927009406316965L;
61         
62     }
63     
64 }

連接池對象:

 1 package com.xinye.test.pool;
 2 
 3 import com.xinye.test.pool.PoolManager.PoolEmptyException;
 4 
 5 /**
 6  * 用戶真正需要關心的池子
 7  * @author xinye
 8  *
 9  */
10 public class ConnectionPool {
11     private static PoolManager manager = new PoolManager();
12     /**
13      * 批量添加連接對象
14      * @param count
15      */
16     public static void addConnections(int count){
17         for(int i = 0;i < count;i++){
18             manager.add(new Connection());
19         }
20     }
21     /**
22      * 得到連接對象
23      * @return
24      * @throws PoolEmptyException
25      */
26     public static IConnection getConnection() throws PoolEmptyException{
27         return manager.get();
28     }
29     /**
30      * 釋放鏈接
31      * @param conn
32      * @throws PoolEmptyException
33      */
34     public static void release(IConnection conn) throws PoolEmptyException{
35         manager.release(conn);
36     }
37 }

對象池(Object Pool)