乐趣区

关于java:reactornetty之client与server如何共用EventLoopGroup

间接到 DefaultLoopResources 类中,能够看到两个办法

    @Override
    public EventLoopGroup onClient(boolean useNative) {if (useNative && LoopResources.hasNativeSupport()) {return cacheNativeClientLoops();
        }
        return cacheNioClientLoops();}
    @Override
    public EventLoopGroup onServer(boolean useNative) {if (useNative && LoopResources.hasNativeSupport()) {return cacheNativeServerLoops();
        }
        return cacheNioServerLoops();}

而后进入 cacheNativeClientLoops() 中

    EventLoopGroup cacheNativeClientLoops() {EventLoopGroup eventLoopGroup = cacheNativeClientLoops.get();
        if (null == eventLoopGroup) {EventLoopGroup newEventLoopGroup = LoopResources.colocate(cacheNativeServerLoops());
            if (!cacheNativeClientLoops.compareAndSet(null, newEventLoopGroup)) {// Do not shutdown newEventLoopGroup as this will shutdown the server loops}
            eventLoopGroup = cacheNativeClientLoops();}
        return eventLoopGroup;
    }

能够看到调了 cacheNativeServerLoops() 办法,实现了与 server 共用 EventLoopGroup.
那么在编程中,如何指定 EventLoopGroup 线程数呢?那么请转到 reactor.netty.resources.LoopResources 中

int DEFAULT_IO_WORKER_COUNT = Integer.parseInt(System.getProperty(
            ReactorNetty.IO_WORKER_COUNT,
            "" + Math.max(Runtime.getRuntime().availableProcessors(), 4)));

其中 ReactorNetty.IO_WORKER_COUNT 值为 ”reactor.netty.ioWorkerCount”, 那能够简略通过设置这个值来实现。

退出移动版