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

View File

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

View File

@@ -130,13 +130,13 @@ public class Context {
} }
public void offerBuffer(ByteBuffer buffer) { public void offerBuffer(ByteBuffer buffer) {
bufferPool.offer(buffer); bufferPool.accept(buffer);
} }
public void offerBuffer(ByteBuffer... buffers) { public void offerBuffer(ByteBuffer... buffers) {
if (buffers == null) return; if (buffers == null) return;
for (ByteBuffer buffer : buffers) { 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; //避免重复关闭 if (!this.inited) return; //避免重复关闭
//System.println("耗时: " + (System.currentTimeMillis() - request.createtime)); //System.println("耗时: " + (System.currentTimeMillis() - request.createtime));
if (kill) refuseAlive(); if (kill) refuseAlive();
this.context.responsePool.offer(this); this.context.responsePool.accept(this);
} }
public void finish(final byte[] bs) { public void finish(final byte[] bs) {

View File

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

View File

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

View File

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