所以,我有我使用一個通用的游泳池,我想知道什麼是真正發生在初始化?是否創建了一位教師並將其傳遞給池以便使用,如果是這樣的話,它具有哪些成本?泛型類 - 初始化在Java中
final Pool<Teacher> pool = new Teacher();
注:池是一個抽象類&教師擴展游泳池 提前感謝!
這裏的游泳池類:
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>();
}
protected abstract T create();
public abstract boolean validate(T o);
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());
}
}
那是不行的,除非'Teacher'延伸'Pool'(或工具) – Justin
這段代碼編譯?這些課程如何看待? –
是的,只要老師擴展池並實現抽象方法,它就可以編譯並運行正常。 – user1861156