ObjectPool实现Consumer接口,且将offer设为过期,建议使用accept方法

This commit is contained in:
Redkale
2017-12-18 10:00:18 +08:00
parent 76c54f8d54
commit 34adb238f7
7 changed files with 33 additions and 27 deletions

View File

@@ -73,7 +73,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
}
public void offerBsonReader(final BsonReader in) {
if (in != null) readerPool.offer(in);
if (in != null) readerPool.accept(in);
}
//------------------------------ writer -----------------------------------------------------------
@@ -90,7 +90,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
}
public void offerBsonWriter(final BsonWriter out) {
if (out != null) writerPool.offer(out);
if (out != null) writerPool.accept(out);
}
//------------------------------ convertFrom -----------------------------------------------------------
@@ -106,7 +106,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
in.setBytes(bytes, start, len);
@SuppressWarnings("unchecked")
T rs = (T) factory.loadDecoder(type).convertFrom(in);
readerPool.offer(in);
readerPool.accept(in);
return rs;
}
@@ -145,7 +145,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
final BsonWriter out = writerPool.get().tiny(tiny);
out.writeNull();
byte[] result = out.toArray();
writerPool.offer(out);
writerPool.accept(out);
return result;
}
return convertTo(value.getClass(), value);
@@ -157,7 +157,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
final BsonWriter out = writerPool.get().tiny(tiny);
factory.loadEncoder(type).convertTo(out, value);
byte[] result = out.toArray();
writerPool.offer(out);
writerPool.accept(out);
return result;
}
@@ -167,7 +167,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
final BsonWriter out = writerPool.get().tiny(tiny);
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
byte[] result = out.toArray();
writerPool.offer(out);
writerPool.accept(out);
return result;
}

View File

@@ -60,7 +60,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
}
public void offerJsonReader(final JsonReader in) {
if (in != null) readerPool.offer(in);
if (in != null) readerPool.accept(in);
}
//------------------------------ writer -----------------------------------------------------------
@@ -81,7 +81,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
}
public void offerJsonWriter(final JsonWriter out) {
if (out != null) writerPool.offer(out);
if (out != null) writerPool.accept(out);
}
//------------------------------ convertFrom -----------------------------------------------------------
@@ -100,7 +100,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
final JsonReader in = readerPool.get();
in.setText(text, start, len);
T rs = (T) factory.loadDecoder(type).convertFrom(in);
readerPool.offer(in);
readerPool.accept(in);
return rs;
}
@@ -142,7 +142,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
final JsonWriter out = writerPool.get().tiny(tiny);
factory.loadEncoder(type).convertTo(out, value);
String result = out.toString();
writerPool.offer(out);
writerPool.accept(out);
return result;
}
@@ -152,7 +152,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
final JsonWriter out = writerPool.get().tiny(tiny);
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
String result = out.toString();
writerPool.offer(out);
writerPool.accept(out);
return result;
}

View File

@@ -130,13 +130,13 @@ public class Context {
}
public void offerBuffer(ByteBuffer buffer) {
bufferPool.offer(buffer);
bufferPool.accept(buffer);
}
public void offerBuffer(ByteBuffer... buffers) {
if (buffers == null) return;
for (ByteBuffer buffer : buffers) {
bufferPool.offer(buffer);
bufferPool.accept(buffer);
}
}

View File

@@ -195,7 +195,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
if (!this.inited) return; //避免重复关闭
//System.println("耗时: " + (System.currentTimeMillis() - request.createtime));
if (kill) refuseAlive();
this.context.responsePool.offer(this);
this.context.responsePool.accept(this);
}
public void finish(final byte[] bs) {

View File

@@ -178,7 +178,7 @@ public final class Transport {
}
public void offerBuffer(ByteBuffer buffer) {
bufferPool.offer(buffer);
bufferPool.accept(buffer);
}
public void offerBuffer(ByteBuffer... buffers) {

View File

@@ -308,7 +308,7 @@ public class TransportFactory {
@Override
public void completed(Integer result, ByteBuffer attachment) {
if (counter > 3) {
bufferPool.offer(attachment);
bufferPool.accept(attachment);
localconn.dispose();
return;
}
@@ -317,7 +317,7 @@ public class TransportFactory {
localconn.read(pongBuffer, pongBuffer, this);
return;
}
bufferPool.offer(attachment);
bufferPool.accept(attachment);
localqueue.offer(localconn);
}

View File

@@ -19,7 +19,7 @@ import java.util.logging.*;
* @author zhangjx
* @param <T> 对象池元素的数据类型
*/
public final class ObjectPool<T> implements Supplier<T> {
public final class ObjectPool<T> implements Supplier<T>, Consumer<T> {
private static final Logger logger = Logger.getLogger(ObjectPool.class.getSimpleName());
@@ -78,21 +78,27 @@ public final class ObjectPool<T> implements Supplier<T> {
return result;
}
public void offer(final T e) {
@Override
public void accept(final T e) {
if (e != null && recycler.test(e)) {
if (cycleCounter != null) cycleCounter.incrementAndGet();
if (debug) {
for (T t : queue) {
if (t == e) {
logger.log(Level.WARNING, "[" + Thread.currentThread().getName() + "] repeat offer the same object(" + e + ")", new Exception());
return;
}
}
}
// if (debug) {
// for (T t : queue) {
// if (t == e) {
// logger.log(Level.WARNING, "[" + Thread.currentThread().getName() + "] repeat offer the same object(" + e + ")", new Exception());
// return;
// }
// }
// }
queue.offer(e);
}
}
@Deprecated
public void offer(final T e) {
accept(e);
}
public long getCreatCount() {
return creatCounter.longValue();
}