From 780029c032b8bbd81917cfbb64ede05d6097bc35 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 20 Aug 2024 18:27:03 +0800 Subject: [PATCH] fix pool --- .../hutool/core/pool/SimplePoolable.java | 53 +++++++++++++++++++ .../pool/partition/PartitionPoolable.java | 27 +++------- .../core/pool/partition/PoolPartition.java | 10 ++-- 3 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 hutool-core/src/main/java/org/dromara/hutool/core/pool/SimplePoolable.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/pool/SimplePoolable.java b/hutool-core/src/main/java/org/dromara/hutool/core/pool/SimplePoolable.java new file mode 100644 index 000000000..8588b40d5 --- /dev/null +++ b/hutool-core/src/main/java/org/dromara/hutool/core/pool/SimplePoolable.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2013-2024 Hutool Team. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hutool.core.pool; + +/** + * 简单可池化对象,此对象会同时持有原始对象和所在的分区 + * + * @param 对象类型 + */ +public class SimplePoolable implements Poolable { + + private final T raw; + private long lastReturn; + + /** + * 构造 + * + * @param raw 原始对象 + */ + public SimplePoolable(final T raw) { + this.raw = raw; + this.lastReturn = System.currentTimeMillis(); + } + + @Override + public T getRaw() { + return this.raw; + } + + @Override + public long getLastReturn() { + return lastReturn; + } + + @Override + public void setLastReturn(final long lastReturn) { + this.lastReturn = lastReturn; + } +} diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/pool/partition/PartitionPoolable.java b/hutool-core/src/main/java/org/dromara/hutool/core/pool/partition/PartitionPoolable.java index 8a2e7342f..c15df96b3 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/pool/partition/PartitionPoolable.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/pool/partition/PartitionPoolable.java @@ -16,18 +16,16 @@ package org.dromara.hutool.core.pool.partition; -import org.dromara.hutool.core.pool.Poolable; +import org.dromara.hutool.core.pool.SimplePoolable; /** * 分区可池化对象,此对象会同时持有原始对象和所在的分区 * * @param 对象类型 */ -public class PartitionPoolable implements Poolable { +public class PartitionPoolable extends SimplePoolable { - private final T raw; private final PoolPartition partition; - private long lastBorrow; /** * 构造 @@ -36,14 +34,8 @@ public class PartitionPoolable implements Poolable { * @param partition 对象所在分区 */ public PartitionPoolable(final T raw, final PoolPartition partition) { - this.raw = raw; + super(raw); this.partition = partition; - this.lastBorrow = System.currentTimeMillis(); - } - - @Override - public T getRaw() { - return this.raw; } /** @@ -53,13 +45,10 @@ public class PartitionPoolable implements Poolable { this.partition.returnObject(this.getRaw()); } - @Override - public long getLastReturn() { - return lastBorrow; - } - - @Override - public void setLastReturn(final long lastReturn) { - this.lastBorrow = lastReturn; + /** + * 释放对象 + */ + public void free() { + this.partition.free(this.getRaw()); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/pool/partition/PoolPartition.java b/hutool-core/src/main/java/org/dromara/hutool/core/pool/partition/PoolPartition.java index f167d3c6b..55bd82e9b 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/pool/partition/PoolPartition.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/pool/partition/PoolPartition.java @@ -16,6 +16,7 @@ package org.dromara.hutool.core.pool.partition; +import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.pool.*; import java.io.IOException; @@ -183,7 +184,8 @@ public class PoolPartition implements ObjectPool { */ protected Poolable createPoolable() { final T t = objectFactory.create(); - return null == t ? null : wrapPoolable(t); + Assert.notNull(t, "Null object created and not allow!"); + return wrapPoolable(t); } @SuppressWarnings("unchecked") @@ -208,12 +210,8 @@ public class PoolPartition implements ObjectPool { } try { - Poolable poolable; for (int i = 0; i < increaseSize; i++) { - poolable = createPoolable(); - if(null != poolable){ - queue.put(poolable); - } + queue.put(createPoolable()); } total += increaseSize; } catch (final InterruptedException e) {