连接池

This commit is contained in:
cheney
2022-10-26 17:44:10 +08:00
parent 24161e2ad5
commit fdadf11c93
7 changed files with 64 additions and 52 deletions
@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
public class ThreadBasedConnectPool<T> {
// ThreadLocal
private final Map<Thread, ObjectInPool<T>> map = new HashMap<>();
private final Map<Thread, ObjectInPool<T>> map = new HashMap<Thread, ObjectInPool<T>>();
// 守护
private final ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(1);
@@ -18,7 +18,7 @@ public class ThreadBasedConnectPool<T> {
// 配置
private PoolConfig config;
public ThreadBasedConnectPool(PoolConfig config) {
public ThreadBasedConnectPool(final PoolConfig config) {
this.config = config;
threadPool.scheduleAtFixedRate(new Runnable() {
@Override
@@ -103,7 +103,7 @@ public class ThreadBasedConnectPool<T> {
// 设置
public void set(T o) {
map.put(Thread.currentThread(), new ObjectInPool<>(o));
map.put(Thread.currentThread(), new ObjectInPool<T>(o));
}
// 清除
@@ -117,7 +117,7 @@ public class ThreadBasedConnectPool<T> {
private T setInitialValue() {
T value = initialValue();
ObjectInPool opi = new ObjectInPool<>(value);
ObjectInPool opi = new ObjectInPool<T>(value);
map.put(Thread.currentThread(), opi);
afterInit( opi , value);
return value;