2024-04-08 10:51:09 +00:00
|
|
|
|
package com.example.catchTheLetters.utils;
|
|
|
|
|
|
2024-04-09 13:24:01 +00:00
|
|
|
|
import lombok.Getter;
|
|
|
|
|
import lombok.Setter;
|
2024-04-08 14:50:55 +00:00
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
2024-04-08 15:02:06 +00:00
|
|
|
|
import java.util.concurrent.ConcurrentLinkedDeque;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
import java.util.function.Supplier;
|
2024-04-08 10:51:09 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2024-04-08 15:10:34 +00:00
|
|
|
|
* 对象池(防止频繁创建对象触发GC)
|
2024-04-10 07:03:57 +00:00
|
|
|
|
*
|
2024-04-08 10:51:09 +00:00
|
|
|
|
* @param <T> 对象类型
|
|
|
|
|
* @author spyn
|
|
|
|
|
*/
|
2024-04-08 15:02:06 +00:00
|
|
|
|
public class ObjectPool<T> implements Iterable<T> {
|
|
|
|
|
/**
|
|
|
|
|
* 对象池
|
|
|
|
|
*/
|
|
|
|
|
private final ConcurrentLinkedDeque<T> pool;
|
2024-04-08 10:51:09 +00:00
|
|
|
|
|
2024-04-09 13:24:01 +00:00
|
|
|
|
/**
|
|
|
|
|
* 重置对象委托
|
|
|
|
|
*/
|
|
|
|
|
private final Consumer<T> reset;
|
|
|
|
|
|
2024-04-08 15:02:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* 创建对象委托
|
|
|
|
|
*/
|
|
|
|
|
private final Supplier<T> factory;
|
|
|
|
|
|
|
|
|
|
/**
|
2024-04-09 13:24:01 +00:00
|
|
|
|
* 对象池上限
|
2024-04-08 15:02:06 +00:00
|
|
|
|
*/
|
2024-04-09 13:24:01 +00:00
|
|
|
|
@Setter
|
|
|
|
|
@Getter
|
|
|
|
|
private Integer limit;
|
|
|
|
|
|
|
|
|
|
/**
|
2024-04-10 07:03:57 +00:00
|
|
|
|
* 是否需要上限
|
2024-04-09 13:24:01 +00:00
|
|
|
|
*/
|
|
|
|
|
@Getter
|
|
|
|
|
@Setter
|
2024-04-10 07:03:57 +00:00
|
|
|
|
private Boolean needLimit;
|
2024-04-09 13:24:01 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当前对象数
|
|
|
|
|
*/
|
|
|
|
|
private Integer count;
|
2024-04-08 15:02:06 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
2024-04-10 07:03:57 +00:00
|
|
|
|
*
|
|
|
|
|
* @param factory 创建对象委托
|
|
|
|
|
* @param reset 重置对象委托
|
|
|
|
|
* @param needLimit 是否需要上限
|
|
|
|
|
* @param limit 对象池上限
|
2024-04-08 15:02:06 +00:00
|
|
|
|
*/
|
2024-04-10 07:03:57 +00:00
|
|
|
|
public ObjectPool(Supplier<T> factory, Consumer<T> reset, Boolean needLimit, Integer limit) {
|
2024-04-08 15:02:06 +00:00
|
|
|
|
this.pool = new ConcurrentLinkedDeque<>();
|
|
|
|
|
this.factory = factory;
|
|
|
|
|
this.reset = reset;
|
2024-04-10 07:03:57 +00:00
|
|
|
|
this.needLimit = needLimit;
|
2024-04-09 13:24:01 +00:00
|
|
|
|
this.limit = limit;
|
|
|
|
|
// 预先创建对象
|
|
|
|
|
for (int i = 0; i < limit; i++) {
|
|
|
|
|
this.pool.push(factory.get());
|
|
|
|
|
}
|
|
|
|
|
count = limit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
2024-04-10 07:03:57 +00:00
|
|
|
|
*
|
2024-04-09 13:24:01 +00:00
|
|
|
|
* @param factory 创建对象委托
|
2024-04-10 07:03:57 +00:00
|
|
|
|
* @param reset 重置对象委托
|
2024-04-09 13:24:01 +00:00
|
|
|
|
*/
|
|
|
|
|
public ObjectPool(Supplier<T> factory, Consumer<T> reset) {
|
|
|
|
|
this(factory, reset, false, 10);
|
2024-04-08 10:51:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取出一个对象
|
2024-04-10 07:03:57 +00:00
|
|
|
|
*
|
2024-04-08 10:51:09 +00:00
|
|
|
|
* @return 对象
|
|
|
|
|
*/
|
2024-04-09 13:24:01 +00:00
|
|
|
|
public T borrowObject() throws InterruptedException {
|
|
|
|
|
if (!pool.isEmpty()) return pool.pop();
|
2024-04-10 07:03:57 +00:00
|
|
|
|
// 如果对象池为空,且此时没到上限,创建新对象,否则如果需要上限约束的情况下返回null
|
|
|
|
|
if (needLimit && count >= limit) return null;
|
|
|
|
|
count++;
|
|
|
|
|
return factory.get();
|
2024-04-08 10:51:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 存入一个对象
|
2024-04-10 07:03:57 +00:00
|
|
|
|
*
|
2024-04-08 10:51:09 +00:00
|
|
|
|
* @param object 对象
|
|
|
|
|
*/
|
|
|
|
|
public void returnObject(T object) {
|
|
|
|
|
if (object == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-08 15:10:34 +00:00
|
|
|
|
// 重置对象
|
2024-04-08 15:06:39 +00:00
|
|
|
|
reset.accept(object);
|
2024-04-10 07:03:57 +00:00
|
|
|
|
this.pool.push(object);
|
2024-04-08 10:51:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 14:50:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* 清空对象池
|
|
|
|
|
*/
|
|
|
|
|
public void clear() {
|
|
|
|
|
this.pool.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 覆写迭代器,便于遍历对象池
|
2024-04-10 07:03:57 +00:00
|
|
|
|
*
|
2024-04-08 15:15:02 +00:00
|
|
|
|
* @return 迭代器
|
2024-04-08 14:50:55 +00:00
|
|
|
|
*/
|
|
|
|
|
@NotNull
|
|
|
|
|
@Override
|
|
|
|
|
public java.util.Iterator<T> iterator() {
|
|
|
|
|
return this.pool.iterator();
|
|
|
|
|
}
|
2024-04-08 10:51:09 +00:00
|
|
|
|
}
|