Netty源码分析第2章(NioEventLoop)---->第3节: 初始化线程选择器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Netty源码分析第2章(NioEventLoop)---->第3节: 初始化线程选择器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2922字,纯文字阅读大概需要5分钟。
内容图文
第二章:NioEventLoop
第三节:初始化线程选择器
回到上一小节的MultithreadEventExecutorGroup类的构造方法:
protected MultithreadEventExecutorGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory, Object... args) { //代码省略 if (executor == null) { //创建一个新的线程执行器(1) executor = new ThreadPerTaskExecutor(newDefaultThreadFactory()); } //构造NioEventLoop(2) children = new EventExecutor[nThreads]; for (int i = 0; i < nThreads; i ++) { boolean success = false; try { children[i] = newChild(executor, args); success = true; } catch (Exception e) { throw new IllegalStateException("failed to create a child event loop", e); } finally { //代码省略 } } //创建线程选择器(3) chooser = chooserFactory.newChooser(children); //代码省略 }
我们看第三步, 创建线程选择器:
chooser = chooserFactory.newChooser(children);
NioEventLoop都绑定一个chooser对象, 作为线程选择器, 通过这个线程选择器, 为每一个channel分配不同的线程
我们看到newChooser(children)传入了NioEventLoop数组, 我们跟到方法中去, 我们跟到了DefaultEventExecutorChooserFactory类中的newChooser方法:
public EventExecutorChooser newChooser(EventExecutor[] executors) { if (isPowerOfTwo(executors.length)) { return new PowerOfTowEventExecutorChooser(executors); } else { return new GenericEventExecutorChooser(executors); } }
这里通过isPowerOfTwo(executors.length)判断NioEventLoop的线程数是不是2的倍数, 然后根据判断结果返回两种选择器对象, 这里使用到java设计模式的策略模式
根据这两个类的名字不难看出, 如果是2的倍数, 使用的是一种高性能的方式选择线程, 如果不是2的倍数, 则使用一种比较普通的线程选择方式
我们简单跟进这两种策略的选择器对象中看一下, 首先看一下PowerOfTowEventExecutorChooser这个类:
private static final class PowerOfTowEventExecutorChooser implements EventExecutorChooser { private final AtomicInteger idx = new AtomicInteger(); private final EventExecutor[] executors; PowerOfTowEventExecutorChooser(EventExecutor[] executors) { this.executors = executors; } @Override public EventExecutor next() { return executors[idx.getAndIncrement() & executors.length - 1]; } }
这个类实现了线程选择器的接口EventExecutorChooser, 构造方法中初始化了NioEventLoop线程数组
重点关注下next()方法, next()方法就是选择下一个线程的方法, 如果线程数是2的倍数, 这里通过按位与进行计算, 所以效率极高
再看一下GenericEventExecutorChooser这个类:
private static final class GenericEventExecutorChooser implements EventExecutorChooser { private final AtomicInteger idx = new AtomicInteger(); private final EventExecutor[] executors; GenericEventExecutorChooser(EventExecutor[] executors) { this.executors = executors; } @Override public EventExecutor next() { return executors[Math.abs(idx.getAndIncrement() % executors.length)]; } }
这个类同样实现了线程选择器的接口EventExecutorChooser, 并在造方法中初始化了NioEventLoop线程数组
再看这个类的next()方法, 如果线程数不是2的倍数, 则用绝对值和取模的这种效率一般的方式进行线程选择
这样, 我们就初始化了线程选择器对象
回到上一小节的MultithreadEventExecutorGroup类的构造方法:
内容总结
以上是互联网集市为您收集整理的Netty源码分析第2章(NioEventLoop)---->第3节: 初始化线程选择器全部内容,希望文章能够帮你解决Netty源码分析第2章(NioEventLoop)---->第3节: 初始化线程选择器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。