This commit is contained in:
Looly 2024-08-20 18:27:03 +08:00
parent 1fa28d6fde
commit 780029c032
3 changed files with 65 additions and 25 deletions

View File

@ -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 <T> 对象类型
*/
public class SimplePoolable<T> implements Poolable<T> {
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;
}
}

View File

@ -16,18 +16,16 @@
package org.dromara.hutool.core.pool.partition; package org.dromara.hutool.core.pool.partition;
import org.dromara.hutool.core.pool.Poolable; import org.dromara.hutool.core.pool.SimplePoolable;
/** /**
* 分区可池化对象此对象会同时持有原始对象和所在的分区 * 分区可池化对象此对象会同时持有原始对象和所在的分区
* *
* @param <T> 对象类型 * @param <T> 对象类型
*/ */
public class PartitionPoolable<T> implements Poolable<T> { public class PartitionPoolable<T> extends SimplePoolable<T> {
private final T raw;
private final PoolPartition<T> partition; private final PoolPartition<T> partition;
private long lastBorrow;
/** /**
* 构造 * 构造
@ -36,14 +34,8 @@ public class PartitionPoolable<T> implements Poolable<T> {
* @param partition 对象所在分区 * @param partition 对象所在分区
*/ */
public PartitionPoolable(final T raw, final PoolPartition<T> partition) { public PartitionPoolable(final T raw, final PoolPartition<T> partition) {
this.raw = raw; super(raw);
this.partition = partition; this.partition = partition;
this.lastBorrow = System.currentTimeMillis();
}
@Override
public T getRaw() {
return this.raw;
} }
/** /**
@ -53,13 +45,10 @@ public class PartitionPoolable<T> implements Poolable<T> {
this.partition.returnObject(this.getRaw()); this.partition.returnObject(this.getRaw());
} }
@Override /**
public long getLastReturn() { * 释放对象
return lastBorrow; */
} public void free() {
this.partition.free(this.getRaw());
@Override
public void setLastReturn(final long lastReturn) {
this.lastBorrow = lastReturn;
} }
} }

View File

@ -16,6 +16,7 @@
package org.dromara.hutool.core.pool.partition; package org.dromara.hutool.core.pool.partition;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.pool.*; import org.dromara.hutool.core.pool.*;
import java.io.IOException; import java.io.IOException;
@ -183,7 +184,8 @@ public class PoolPartition<T> implements ObjectPool<T> {
*/ */
protected Poolable<T> createPoolable() { protected Poolable<T> createPoolable() {
final T t = objectFactory.create(); 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") @SuppressWarnings("unchecked")
@ -208,12 +210,8 @@ public class PoolPartition<T> implements ObjectPool<T> {
} }
try { try {
Poolable<T> poolable;
for (int i = 0; i < increaseSize; i++) { for (int i = 0; i < increaseSize; i++) {
poolable = createPoolable(); queue.put(createPoolable());
if(null != poolable){
queue.put(poolable);
}
} }
total += increaseSize; total += increaseSize;
} catch (final InterruptedException e) { } catch (final InterruptedException e) {