Netty EventLoop与EventExecutor分析(1)
来源:互联网 发布:淘宝管控记录是什么 编辑:程序博客网 时间:2024/05/16 23:38
现在算是正式开始进入Netty的源码了吧,首先从事件循环的执行部分开始吧,上图就是顶层的几个接口之间的关系,首先我们来看最上层的EventExecutorGroup
public interface EventExecutorGroup extends ScheduledExecutorService, Iterable<EventExecutor> { /** * Returns {@code true} if and only if this executor was started to be * {@linkplain #shutdownGracefully() shut down gracefuclly} or was {@linkplain #isShutdown() shut down}. */ boolean isShuttingDown(); // /** * Shortcut method for {@link #shutdownGracefully(long, long, TimeUnit)} with sensible default values. */ void shutdownGracefully(); /** * Signals this executor that the caller wants the executor to be shut down. Once this method is called, * {@link #isShuttingDown()} starts to return {@code true}, and the executor prepares to shut itself down. * Unlike {@link #shutdown()}, graceful shutdown ensures that no tasks are submitted for <i>'the quiet period'</i> * (usually a couple seconds) before it shuts itself down. If a task is submitted during the quiet period, * it is guaranteed to be accepted and the quiet period will start over. * * @param quietPeriod the quiet period as described in the documentation * @param timeout the maximum amount of time to wait until the executor is {@linkplain #shutdown()} * regardless if a task was submitted during the quiet period * @param unit the unit of {@code quietPeriod} and {@code timeout} */ void shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit); /** * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead. */ @Override @Deprecated void shutdown(); /** * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead. */ @Override @Deprecated List<Runnable> shutdownNow(); /** * Returns one of the {@link EventExecutor}s that belong to this group. */ EventExecutor next(); /** * Returns a read-only {@link Iterator} over all {@link EventExecutor}, which are handled by this * {@link EventExecutorGroup} at the time of invoke this method. */ @Override Iterator<EventExecutor> iterator(); @Override Future<?> submit(Runnable task); @Override <T> Future<T> submit(Runnable task, T result); @Override <T> Future<T> submit(Callable<T> task); @Override ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); @Override <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); @Override ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); @Override ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);}这个接口定义的相对来说还是很简单的,因为一看就基本能看明白每个方法的作用,首先它继承了ScheduleEcecutorService ,因此可以把它看成是一个Task的执行器,另外又实现了Iterable接口,就可以看出它的目的也可以当成一个容器(EventExecutor的容器)。。。
方法也都比较基础,无非就是一些提交任务,取出可用的EventExecutor,关闭执行器等等。。。
接下来来看EventExecutor接口的定义
public interface EventExecutor extends EventExecutorGroup { /** * Returns a reference to itself. */ @Override EventExecutor next(); /** * Return the {@link EventExecutorGroup} which is the parent of this {@link EventExecutor}, */ EventExecutorGroup parent(); /** * Calls {@link #inEventLoop(Thread)} with {@link Thread#currentThread()} as argument */ boolean inEventLoop(); /** * Return {@code true} if the given {@link Thread} is executed in the event loop, * {@code false} otherwise. */ boolean inEventLoop(Thread thread); /** * Return a new {@link Promise}. */ <V> Promise<V> newPromise(); /** * Create a new {@link ProgressivePromise}. */ <V> ProgressivePromise<V> newProgressivePromise(); /** * Create a new {@link Future} which is marked as successes already. So {@link Future#isSuccess()} * will return {@code true}. All {@link FutureListener} added to it will be notified directly. Also * every call of blocking methods will just return without blocking. */ <V> Future<V> newSucceededFuture(V result); /** * Create a new {@link Future} which is marked as fakued already. So {@link Future#isSuccess()} * will return {@code false}. All {@link FutureListener} added to it will be notified directly. Also * every call of blocking methods will just return without blocking. */ <V> Future<V> newFailedFuture(Throwable cause);}它继承了EventExecutorGroup类,因此也可以将它堪称是一个任务的执行器,不过稍微有点不同的是它的next方法返回的是自己的一个引用,
接下来再来看EventLoopGroup的定义:
public interface EventLoopGroup extends EventExecutorGroup { /** * Return the next {@link EventLoop} to use */ @Override EventLoop next(); /** * Register a {@link Channel} with this {@link EventLoop}. The returned {@link ChannelFuture} * will get notified once the registration was complete. */ ChannelFuture register(Channel channel); /** * Register a {@link Channel} with this {@link EventLoop}. The passed {@link ChannelFuture} * will get notified once the registration was complete and also will get returned. */ ChannelFuture register(Channel channel, ChannelPromise promise);}它的定义更简单,不过这里的next方法返回的是空闲的EventLoop
最后来看EventLoop的定义:
public interface EventLoop extends EventExecutor, EventLoopGroup { @Override EventLoopGroup parent();}该接口同时继承了EventExecutor和EventLoopGroup,因此它既是一个执行器,又是容器。。。
- Netty EventLoop与EventExecutor分析(1)
- 自顶向下深入分析Netty(四)--EventLoop-1
- Netty 4 源码分析——EventExecutor
- netty EventLoop 源码分析(一)
- 自顶向下深入分析Netty(四)--EventLoop-2
- [netty源码分析]--EventLoopGroup与EventLoop 分析netty的线程模型
- netty源码分析 之六 transport(EventLoop)
- Netty 源码分析之 EventLoop(一)
- Netty 源码分析之 EventLoop(二) (最重要)
- netty EventLoop write() 源码分析(二)
- EventLoop分析[1-4]
- 《Netty学习》(七)EventLoop学习
- netty源码学习二(EventLoopGroup、EventLoop)
- Netty源码分析之EventLoop相关结构分析
- netty(八)源码分析之eventLoop和eventLoopGroup
- Netty源码分析之三【我就是大名鼎鼎的 EventLoop】
- squid源码分析1 ----EventLoop
- Netty 学习 - EventLoop
- (通过客服端传数据)spring设置动态定时任务
- 马尔可夫模型(Markov models)
- 动态规划---->可靠性设计
- hdu2026首字母变大写
- 移动消息应用迅速崛起 Facebook已经过时了?
- Netty EventLoop与EventExecutor分析(1)
- 第四部分 个人理财风险防范10.当心新的金融诈骗:金蝉脱壳
- 线性规划与网络流24题
- 驱动调试常见问题_触摸屏
- android布局页面中引入另一个布局页面 .
- Solution to error: ‘_1’ was not declared in this scope under C++11 standard
- 数字键盘添加自定义按钮
- Ubuntu 下常用的软件工具
- 方法重载和重写的区别