亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java設(shè)計(jì)模式之實(shí)現(xiàn)對象池模式示例分享

 更新時(shí)間:2014年02月18日 11:08:38   作者:  
對象池模式經(jīng)常用在頻繁創(chuàng)建、銷毀對象(并且對象創(chuàng)建、銷毀開銷很大)的場景,比如數(shù)據(jù)庫連接池、線程池、任務(wù)隊(duì)列池等。本代碼簡單,沒有限制對象池大小

ObjectPool抽象父類

復(fù)制代碼 代碼如下:

import java.util.Iterator;
import java.util.Vector;

public abstract class ObjectPool<T> {

   private Vector<T> locked, unlocked;   // locked是已占用的對象集合,unlocked是可用對象集合

   public ObjectPool() {
    locked = new Vector<T>();
    unlocked = new Vector<T>();
   }

   // 創(chuàng)建對象
   protected abstract T create();

   // 驗(yàn)證對象有效性
   public abstract boolean validate(T o);

   // 使對象失效
   public abstract void expire(T o);

   // 檢出:從對象池獲取對象
   public synchronized T checkOut() {
    T t;
    if (unlocked.size() > 0) {
     Iterator<T> iter = unlocked.iterator();
     while(iter.hasNext()) {
      t = iter.next();
      if(validate(t)) {   // 對象有效
       unlocked.remove(t);
       locked.add(t);

       return t;
      }
      else {   // 對象已經(jīng)失效
       unlocked.remove(t);
       expire(t);
      }
     }
    }

    // 對象池塘沒有可用對象,創(chuàng)建新對象
    t = create();
    locked.add(t);

    return (t);
   }

   // 檢入:釋放對象回對象池
   public synchronized void checkIn(T t) {
    locked.remove(t);
    if(validate(t)) {   // 如果對象仍有效則放回可用對象集合中
     unlocked.add(t);
    }
    else {   // 否則使對象失效
     expire(t);
    }
   }

}

JDBCConnectionPool子類

復(fù)制代碼 代碼如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCConnectionPool extends ObjectPool<Connection> {

 private String url, usr, pwd;

 public JDBCConnectionPool(String driver, String url, String usr, String pwd) {
  super();

  // 加載對應(yīng)的數(shù)據(jù)庫驅(qū)動(dòng)
  try {
   Class.forName(driver).newInstance();
  }
  catch(Exception e) {
   e.printStackTrace();
  }

  this.url = url;
  this.usr = usr;
  this.pwd = pwd;
 }

 @Override
 protected Connection create() {
  try {
   return DriverManager.getConnection(url, usr, pwd);
  }
  catch(SQLException e) {
   e.printStackTrace();
  }

  return null;
 }

 @Override
 public boolean validate(Connection o) {
  try {
   return o.isClosed();
  }
  catch(SQLException e) {
   e.printStackTrace();
  }

  return false;
 }

 @Override
 public void expire(Connection o) {
  try {
   o.close();
  }
  catch(SQLException e) {
   e.printStackTrace();
  }
  finally {
   o = null;
  }
 }

 public static void main(String[] args) {
  JDBCConnectionPool dbConnPool = new JDBCConnectionPool("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1:3306/test", "root", "123");

  // 獲取數(shù)據(jù)庫連接對象
  Connection conn = dbConnPool.checkOut();

  // 使用數(shù)據(jù)庫連接對象
  // ...

  // 釋放數(shù)據(jù)庫連接對象
  dbConnPool.checkIn(conn);

 }

}

復(fù)制代碼 代碼如下:

class Pool {
   private static final MAX_AVAILABLE = 100;
   private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);

   public Object getItem() throws InterruptedException {
     available.acquire();
     return getNextAvailableItem();
   }

   public void putItem(Object x) {
     if (markAsUnused(x))
       available.release();
   }

   // Not a particularly efficient data structure; just for demo

   protected Object[] items = ... whatever kinds of items being managed
   protected boolean[] used = new boolean[MAX_AVAILABLE];

   protected synchronized Object getNextAvailableItem() {
     for (int i = 0; i < MAX_AVAILABLE; ++i) {
       if (!used[i]) {
          used[i] = true;
          return items[i];
       }
     }
     return null; // not reached
   }

   protected synchronized boolean markAsUnused(Object item) {
     for (int i = 0; i < MAX_AVAILABLE; ++i) {
       if (item == items[i]) {
          if (used[i]) {
            used[i] = false;
            return true;
          } else
            return false;
       }
     }
     return false;
   }

 }

相關(guān)文章

最新評論