稳定性升级
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package org.kne.cloud.network.klalb;
|
||||
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class ByteArrayRecycle {
|
||||
private ConcurrentLinkedQueue<byte[]>rec=new ConcurrentLinkedQueue<>();
|
||||
private int capacity;
|
||||
private int length;
|
||||
public ByteArrayRecycle(int capacity, int length) {
|
||||
super();
|
||||
this.capacity = capacity;
|
||||
this.length = length;
|
||||
}
|
||||
public synchronized void recycle(byte[]b) {
|
||||
if(b.length!=length)
|
||||
throw new IllegalArgumentException("wrong length");
|
||||
if(rec.size()<capacity) {
|
||||
rec.add(b);
|
||||
}
|
||||
}
|
||||
public synchronized byte[] create() {
|
||||
byte[]b=rec.poll();
|
||||
if(b==null) {
|
||||
b=new byte[length];
|
||||
}
|
||||
return b;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user