From b83f6867f34467c0d6e6128b579ce19544efb521 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Tue, 19 Jan 2021 17:00:28 +0800 Subject: [PATCH] --- src/org/redkale/net/TcpNioProtocolServer.java | 2 +- src/org/redkale/net/nio/NioThread.java | 5 +---- src/org/redkale/net/nio/NioThreadGroup.java | 7 +++++-- src/org/redkale/util/ObjectPool.java | 18 +++++++++++++++++- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/org/redkale/net/TcpNioProtocolServer.java b/src/org/redkale/net/TcpNioProtocolServer.java index c69b772c7..efec5fc2d 100644 --- a/src/org/redkale/net/TcpNioProtocolServer.java +++ b/src/org/redkale/net/TcpNioProtocolServer.java @@ -94,7 +94,7 @@ public class TcpNioProtocolServer extends ProtocolServer { this.responsePool = server.createResponsePool(createResponseCounter, cycleResponseCounter, server.responsePoolSize); this.responsePool.setCreator(server.createResponseCreator(bufferPool, responsePool)); - this.ioGroup = new NioThreadGroup(Runtime.getRuntime().availableProcessors(), context.executor, bufferPool); + this.ioGroup = new NioThreadGroup(Runtime.getRuntime().availableProcessors(), bufferPool); this.ioGroup.start(); this.acceptThread = new Thread() { diff --git a/src/org/redkale/net/nio/NioThread.java b/src/org/redkale/net/nio/NioThread.java index fbc1d8344..75fd9f2e7 100644 --- a/src/org/redkale/net/nio/NioThread.java +++ b/src/org/redkale/net/nio/NioThread.java @@ -27,8 +27,6 @@ public class NioThread extends Thread { final Selector selector; - private final ExecutorService executor; - private final ObjectPool bufferPool; private final ConcurrentLinkedQueue> registers = new ConcurrentLinkedQueue<>(); @@ -37,10 +35,9 @@ public class NioThread extends Thread { private boolean closed; - public NioThread(Selector selector, ExecutorService executor, ObjectPool bufferPool) { + public NioThread(Selector selector, ObjectPool bufferPool) { super(); this.selector = selector; - this.executor = executor; this.bufferPool = bufferPool; this.setDaemon(true); } diff --git a/src/org/redkale/net/nio/NioThreadGroup.java b/src/org/redkale/net/nio/NioThreadGroup.java index f23559168..0dc84a7c8 100644 --- a/src/org/redkale/net/nio/NioThreadGroup.java +++ b/src/org/redkale/net/nio/NioThreadGroup.java @@ -30,10 +30,13 @@ public class NioThreadGroup { private ScheduledThreadPoolExecutor timeoutExecutor; - public NioThreadGroup(int threads, ExecutorService executor, ObjectPool bufferPool) throws IOException { + public NioThreadGroup(int threads, ObjectPool bufferPool) throws IOException { this.threads = new NioThread[Math.max(threads, 1)]; for (int i = 0; i < this.threads.length; i++) { - this.threads[i] = new NioThread(Selector.open(), executor, bufferPool); + ObjectPool threadBufferPool = ObjectPool.createUnsafePool(bufferPool.getCreatCounter(), + bufferPool.getCycleCounter(), 8, + bufferPool.getCreator(), bufferPool.getPrepare(), bufferPool.getRecycler()); + this.threads[i] = new NioThread(Selector.open(), threadBufferPool); } this.timeoutExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1, (Runnable r) -> { Thread t = new Thread(r); diff --git a/src/org/redkale/util/ObjectPool.java b/src/org/redkale/util/ObjectPool.java index f32c03ffb..d0a4aab40 100644 --- a/src/org/redkale/util/ObjectPool.java +++ b/src/org/redkale/util/ObjectPool.java @@ -130,10 +130,26 @@ public class ObjectPool implements Supplier, Consumer { return this.creator; } - public Predicate getRecyclerPredicate() { + public int getMax() { + return max; + } + + public Consumer getPrepare() { + return prepare; + } + + public Predicate getRecycler() { return recycler; } + public AtomicLong getCreatCounter() { + return creatCounter; + } + + public AtomicLong getCycleCounter() { + return cycleCounter; + } + @Override public T get() { T result = queue.poll();