mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-13 18:08:35 +08:00
增加ByteBufferPool
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user