对象池

This commit is contained in:
石皮幼鸟 2024-04-08 22:50:55 +08:00
parent 48594250e3
commit 6ff8c6be75
1 changed files with 24 additions and 1 deletions

View File

@ -1,5 +1,7 @@
package com.example.catchTheLetters.utils;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
@ -7,7 +9,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
* @param <T> 对象类型
* @author spyn
*/
public abstract class ObjectPool<T> {
public abstract class ObjectPool<T> implements Iterable<T> {
private final ConcurrentLinkedQueue<T> pool;
public ObjectPool() {
@ -42,4 +44,25 @@ public abstract class ObjectPool<T> {
* @return 新建的对象
*/
protected abstract T createObject();
/**
* 清空对象池
*/
public void clear() {
this.pool.clear();
}
/**
* 覆写迭代器便于遍历对象池
*/
@NotNull
@Override
public java.util.Iterator<T> iterator() {
return this.pool.iterator();
}
@Override
public String toString() {
return this.pool.toString();
}
}