增加ByteBufferPool

This commit is contained in:
free will
2021-11-19 16:29:54 +08:00
parent b14e137cba
commit 7ebbfb88c2
+35
View File
@@ -0,0 +1,35 @@
package util;
import java.nio.ByteBuffer;
import java.util.concurrent.ConcurrentLinkedQueue;
/*
* @Author: Wang Feng
* @Description: 减少堆内存的消耗
* @Version: 1.0.0
* @Date: 15:21 2021/11/19
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ByteBufferPool {
private static final int BUFFER_SIZE = 16384;
private static ConcurrentLinkedQueue<ByteBuffer> pool = new ConcurrentLinkedQueue<>();
public static ByteBuffer acquire()
{
ByteBuffer buffer = pool.poll();
if (buffer == null)
buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); // Using DirectBuffer for zero-copy
return buffer;
}
public static void release(ByteBuffer buffer)
{
buffer.clear();
pool.offer(buffer);
}
public static void clear()
{
pool.clear();
}
}