From fb6382a57a7d8e875eb95324e95255f787520d57 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Tue, 2 Jul 2019 02:14:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=B1=A0=E6=97=A0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biz/filters/bean/GenericServicePool.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/bean/GenericServicePool.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/bean/GenericServicePool.java index 34afeaf..2d94647 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/bean/GenericServicePool.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/bean/GenericServicePool.java @@ -22,10 +22,9 @@ public class GenericServicePool { private class GenericServiceManager { - Lock lock = new ReentrantLock(); + Lock idleLock = new ReentrantLock(); LinkedList idle = new LinkedList<>(); - LinkedList busy = new LinkedList<>(); private String key; @@ -50,44 +49,54 @@ public class GenericServicePool { public GenericService get() { try { - if(lock.tryLock(15L, TimeUnit.SECONDS)) { + if (idleLock.tryLock(15L, TimeUnit.SECONDS)) { + while (idle.isEmpty()) { + idle.wait(); + } return idle.pop(); } else { log.error(this.key + ": 超时{}秒", 15); } } catch (Exception e) { - //TODO: handle exception + // TODO: handle exception } finally { - lock.unlock(); + idleLock.unlock(); } return null; } - - + public void add(GenericService gs) { + idle.add(gs); + if (idle.isEmpty()) { + idle.notify(); + } + } public GenericServiceManager() { - idle = new LinkedList<>(); - busy = new LinkedList<>(); } } - HashMap gsDictionary; public GenericServicePool() { gsDictionary = new HashMap(); } - - 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); } } \ No newline at end of file