netty5 NioEventLoopGroup线程池简析
2016-01-03 21:15
239 查看
netty服务端ServerBootstrap 启动的时候,默认有两个eventloop分别是
bossGroup和 workGroup. (其实大部分人这么叫的.并没有明确这个名字).
一般服务端启动的时候会使用下面的代码
跟踪一下就可以发现
其实默认线程数是 cpu核心数的2倍. 但是也可以通过
bossGroup和 workGroup. (其实大部分人这么叫的.并没有明确这个名字).
一般服务端启动的时候会使用下面的代码
NioEventLoopGroup bossGroup1 = new NioEventLoopGroup(1);//boss NioEventLoopGroup workerGroup = new NioEventLoopGroup();//worker try { ServerBootstrap b = new ServerBootstrap(); ((ServerBootstrap)((ServerBootstrap)((ServerBootstrap)b.group(bossGroup1, workerGroup).channel(NioServerSocketChannel.class)).option(ChannelOption.SO_BACKLOG, Integer.valueOf(100))).handler(new LoggingHandler(LogLevel.INFO))).childHandler(new ChannelInitializer() { public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if(sslCtx != null) { p.addLast(new ChannelHandler[]{sslCtx.newHandler(ch.alloc())}); } p.addLast(new ChannelHandler[]{new EchoServerHandler()}); } }); ChannelFuture f = b.bind(PORT).sync(); f.channel().closeFuture().sync();
其中boss用来监控tcp链接,worker用来处理io事件. 具体的说,boss执行 server.accept()操作 .worker处理事件的读写到业务逻辑处理等后续操作.
NioEventLoopGroup 和NioEventLoop 都可以.但是前者使用的是线程池. 其实bossgroup如果服务端开启的是一个端口(大部分都是一个),单线程即可. worker大部分情况需要多线程处理了 .因为 一个eventloop绑定了一个selector,事件都是通过selector轮询处理的. 一万个情况让一个select处理和让100个selector处理 肯定是多线程效率要高一些(因为有io). 那么下面就讨论一下 ,eventloopgroup的默认线程个数. 请看下面代码
//----开始调用--- NioEventLoopGroup workerGroup = new NioEventLoopGroup(); //默认调用方法 public NioEventLoopGroup() { this(0); } //-----链1 public NioEventLoopGroup(int nEventLoops, Executor executor) { this(nEventLoops, executor, SelectorProvider.provider()); } //------连2 public NioEventLoopGroup(int nEventLoops, Executor executor, SelectorProvider selectorProvider) { super(nEventLoops, executor, new Object[]{selectorProvider}); } //连3------ /** * @see {@link MultithreadEventExecutorGroup#MultithreadEventExecutorGroup(int, Executor, Object...)} */ protected MultithreadEventLoopGroup(int nEventLoops, Executor executor, Object... args) { super(nEventLoops == 0 ? DEFAULT_EVENT_LOOP_THREADS : nEventLoops, executor, args); } //---------连4 private static final int DEFAULT_EVENT_LOOP_THREADS; static { DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt( "io.netty.eventLoopThreads", Runtime.getRuntime().availableProcessors() * 2)); if (logger.isDebugEnabled()) { logger.debug("-Dio.netty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS); } }
跟踪一下就可以发现
其实默认线程数是 cpu核心数的2倍. 但是也可以通过
-Dio.netty.eventLoopThreads 参数在服务端启动的时候指定 .
相关文章推荐
- 优秀的架构师应该具备哪些能力
- linux下which,whereis,locate,find用法
- 细说五层网站架构
- linux禁止root用户直接登录sshd并修改默认端口
- linux中which whereis locate find 的用法
- centos系统下pip安装使用详解
- linux下gcc/g++的命令和使用详解
- shell编程基础
- 20个命令行工具监控 Linux 系统性能(转载)
- linux虚拟机重新启动后不能联网的问题
- linux实战案例-1
- 混合、抗锯齿、雾、多边形偏移及显示列表(openGL)
- 在VirtualBox 5.0.12 for Linux版本上安装CentOS 6.6 x86_64系统
- Linux学习之Led驱动程序理解
- centos 系统启动流程
- 客户端putty, xshell连接linux中vim的小键盘问题
- linux内核学习(一)------------模块
- Linux阵列 RAID详解
- Tomcat中的设计模式--外观模式(门店模式)
- linux 使用fdisk分区扩容