我正在尝试实现对象池,该对象池具有固定数量的对象供池使用,并在池为空时使用wait,并在线程释放对象时通知。
我可以使用以下程序实现上述必需的功能。
我想知道实施是否正确或需要从Interview角度进行任何修改?
import java.util.ArrayList;
import java.util.List;
class ObjectPool
{
static List objects = new ArrayList();
static
{
objects.add("Object One");
objects.add("Object Two");
}
public Object getObject()
{
synchronized(objects)
{
if(objects.isEmpty())
{
System.out.println(Thread.currentThread().getName() + " waiting as Object Pool is empty");
try {
objects.wait();
System.out.println(Thread.currentThread().getName() + " Got Notification");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Object locked = objects.get(objects.size()-1);
objects.remove(locked);
System.out.println(Thread.currentThread().getName() + " got lock of object : "+ locked);
return locked;
}
}
public boolean release(Object released)
{
synchronized(objects)
{
System.out.println(Thread.currentThread().getName() + " releasing Object : "+released);
objects.notify();
return objects.add(released);
}
}
}
public class MainforObjectPool implements Runnable
{
static ObjectPool p = new ObjectPool();
public static void main(String[] args)
{
MainforObjectPool m = new MainforObjectPool();
Thread t1 = new Thread(m,"thread 1");
Thread t2 = new Thread(m,"thread 2");
Thread t3 = new Thread(m,"thread 3");
Thread t4 = new Thread(m,"thread 4");
Thread t5 = new Thread(m,"thread 5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
System.out.println("Main Thread Completed");
}
public void run()
{
Object locked = p.getObject();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.release(locked);
}
}
答案 0 :(得分:0)
很久以前,我使用与您的抽象相似的抽象解决了类似的问题。幸运的是,我将其保存并放入here。为了使这个答案简短,希望能用我的话说清楚,我不会在此处发布整个代码。
您可以拥有一个具有泛型的抽象类,该类可以使您创建自己喜欢的对象的池。是的,我会像这样使用我的最爱并摆脱它
此抽象类允许其实现者处理创建/过期对象的逻辑。
此类具有锁定和未锁定的对象的队列。
当您checkIn
进入池中的对象时,该对象将进入解锁队列中。
当您checkOut
从池中访问对象时,它将通过调用抽象方法validate()
来检查该对象是否到期。如果此函数返回true
,则对象将移至锁定队列。如果此函数返回false
,则通过调用抽象函数expire()
将对象从池中删除并过期(您可以在其中使用notify
)。如果要检查的对象没有被合并,则将其创建并放入锁定队列中。
public abstract class ObjectPool<T> {
private long expirationTime;
private Hashtable<T, Long> locked, unlocked;
public ObjectPool() {
expirationTime = 30000; // 30 seconds
locked = new Hashtable<T, Long>();
unlocked = new Hashtable<T, Long>();
}
/**
* Implemented in concrete class. Create an object to be pooled.
*/
protected abstract T create();
/**
* Used to check whether the object should be kept in the lock, or released.
*/
public abstract boolean validate(T o);
/**
* Object expired. (Use notify?)
*/
public abstract void expire(T o);
public synchronized T checkOut() {
long now = System.currentTimeMillis();
T t;
if (unlocked.size() > 0) {
Enumeration<T> e = unlocked.keys();
while (e.hasMoreElements()) {
t = e.nextElement();
if ((now - unlocked.get(t)) > expirationTime) {
// object has expired
unlocked.remove(t);
expire(t);
t = null;
} else {
if (validate(t)) {
unlocked.remove(t);
locked.put(t, now);
return (t);
} else {
// object failed validation
unlocked.remove(t);
expire(t);
t = null;
}
}
}
}
// no objects available, create a new one
t = create();
locked.put(t, now);
return (t);
}
public synchronized void checkIn(T t) {
locked.remove(t);
unlocked.put(t, System.currentTimeMillis());
}
public synchronized long getExpirationTime() {
return expirationTime;
}
public synchronized void setExpirationTime(long expirationTime) {
this.expirationTime = expirationTime;
}
}