CatchTheLettersBackend/src/main/java/com/example/catchTheLetters/utils/ObjectPool.java

128 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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