对象池无测试实现
This commit is contained in:
parent
1b0f865003
commit
fb6382a57a
|
@ -22,10 +22,9 @@ public class GenericServicePool {
|
||||||
|
|
||||||
private class GenericServiceManager {
|
private class GenericServiceManager {
|
||||||
|
|
||||||
Lock lock = new ReentrantLock();
|
Lock idleLock = new ReentrantLock();
|
||||||
|
|
||||||
LinkedList<GenericService> idle = new LinkedList<>();
|
LinkedList<GenericService> idle = new LinkedList<>();
|
||||||
LinkedList<GenericService> busy = new LinkedList<>();
|
|
||||||
|
|
||||||
private String key;
|
private String key;
|
||||||
|
|
||||||
|
@ -50,44 +49,54 @@ public class GenericServicePool {
|
||||||
|
|
||||||
public GenericService get() {
|
public GenericService get() {
|
||||||
try {
|
try {
|
||||||
if(lock.tryLock(15L, TimeUnit.SECONDS)) {
|
if (idleLock.tryLock(15L, TimeUnit.SECONDS)) {
|
||||||
|
while (idle.isEmpty()) {
|
||||||
|
idle.wait();
|
||||||
|
}
|
||||||
return idle.pop();
|
return idle.pop();
|
||||||
} else {
|
} else {
|
||||||
log.error(this.key + ": 超时{}秒", 15);
|
log.error(this.key + ": 超时{}秒", 15);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//TODO: handle exception
|
// TODO: handle exception
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
idleLock.unlock();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void add(GenericService gs) {
|
||||||
|
idle.add(gs);
|
||||||
|
if (idle.isEmpty()) {
|
||||||
|
idle.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public GenericServiceManager() {
|
public GenericServiceManager() {
|
||||||
idle = new LinkedList<>();
|
|
||||||
busy = new LinkedList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
HashMap<String, GenericServiceManager> gsDictionary;
|
HashMap<String, GenericServiceManager> gsDictionary;
|
||||||
|
|
||||||
public GenericServicePool() {
|
public GenericServicePool() {
|
||||||
gsDictionary = new HashMap<String, GenericServiceManager>();
|
gsDictionary = new HashMap<String, GenericServiceManager>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public GenericService get(String key) {
|
public GenericService get(String key) {
|
||||||
|
GenericServiceManager pool = gsDictionary.get(key);
|
||||||
|
return pool.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public GenericService add(String key, Object genericService) {
|
public void add(String key, GenericService genericService) {
|
||||||
|
GenericServiceManager pool;
|
||||||
|
if (gsDictionary.containsKey(key)) {
|
||||||
|
pool = gsDictionary.get(key);
|
||||||
|
} else {
|
||||||
|
pool = new GenericServiceManager();
|
||||||
|
gsDictionary.put(key, pool);
|
||||||
|
}
|
||||||
|
pool.add(genericService);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user