对象池
This commit is contained in:
parent
48594250e3
commit
6ff8c6be75
|
@ -1,5 +1,7 @@
|
||||||
package com.example.catchTheLetters.utils;
|
package com.example.catchTheLetters.utils;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,7 +9,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
* @author spyn
|
* @author spyn
|
||||||
*/
|
*/
|
||||||
public abstract class ObjectPool<T> {
|
public abstract class ObjectPool<T> implements Iterable<T> {
|
||||||
private final ConcurrentLinkedQueue<T> pool;
|
private final ConcurrentLinkedQueue<T> pool;
|
||||||
|
|
||||||
public ObjectPool() {
|
public ObjectPool() {
|
||||||
|
@ -42,4 +44,25 @@ public abstract class ObjectPool<T> {
|
||||||
* @return 新建的对象
|
* @return 新建的对象
|
||||||
*/
|
*/
|
||||||
protected abstract T createObject();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue