间接到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", 那能够简略通过设置这个值来实现。