Package org.miaixz.bus.core.center.queue
Class MemorySafeLinkedBlockingQueue<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.LinkedBlockingQueue<E>
org.miaixz.bus.core.center.queue.CheckedLinkedBlockingQueue<E>
org.miaixz.bus.core.center.queue.MemorySafeLinkedBlockingQueue<E>
- Type Parameters:
E- 元素类型
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,BlockingQueue<E>,Queue<E>
内存安全的
LinkedBlockingQueue,可以解决OOM问题。
原理是通过Runtime#freeMemory()获取剩余内存,当剩余内存低于指定的阈值时,不再加入。
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
Fields inherited from class org.miaixz.bus.core.center.queue.CheckedLinkedBlockingQueue
checker -
Constructor Summary
ConstructorsConstructorDescriptionMemorySafeLinkedBlockingQueue(long maxFreeMemory) 构造MemorySafeLinkedBlockingQueue(Collection<? extends E> c, long maxFreeMemory) 构造 -
Method Summary
Modifier and TypeMethodDescriptionlongget the max free memory.voidsetMaxFreeMemory(int maxFreeMemory) set the max free memory.Methods inherited from class org.miaixz.bus.core.center.queue.CheckedLinkedBlockingQueue
offer, offer, putMethods inherited from class java.util.concurrent.LinkedBlockingQueue
clear, contains, drainTo, drainTo, forEach, iterator, peek, poll, poll, remainingCapacity, remove, removeAll, removeIf, retainAll, size, spliterator, take, toArray, toArray, toStringMethods inherited from class java.util.AbstractQueue
add, addAll, element, removeMethods inherited from class java.util.AbstractCollection
containsAll, isEmptyMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.BlockingQueue
addMethods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, stream, toArray
-
Constructor Details
-
MemorySafeLinkedBlockingQueue
public MemorySafeLinkedBlockingQueue(long maxFreeMemory) 构造- Parameters:
maxFreeMemory- 最大剩余内存大小,当实际内存小于这个值时,不再加入元素
-
MemorySafeLinkedBlockingQueue
构造- Parameters:
c- 初始集合maxFreeMemory- 最大剩余内存大小,当实际内存小于这个值时,不再加入元素
-
-
Method Details
-
getMaxFreeMemory
public long getMaxFreeMemory()get the max free memory.- Returns:
- the max free memory limit
-
setMaxFreeMemory
public void setMaxFreeMemory(int maxFreeMemory) set the max free memory.- Parameters:
maxFreeMemory- the max free memory
-