java设计模式之实现对象池模式示例分享
objectpool抽象父类
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>();
}
// 创建对象
protected abstract t create();
// 验证对象有效性
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 { // 对象已经失效
unlocked.remove(t);
expire(t);
}
}
}
// 对象池塘没有可用对象,创建新对象
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子类
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();
// 加载对应的数据库驱动
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");
// 获取数据库连接对象
connection conn = dbconnpool.checkout();
// 使用数据库连接对象
// ...
// 释放数据库连接对象
dbconnpool.checkin(conn);
}
}
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;
}
}
上一篇: mysql like查询字符串示例语句
下一篇: 如何恢复Mysql数据库的详细介绍