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,因此它既是一个执行器,又是容器。。。


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我12爸妈离婚该怎么办 苹果迅雷下载的链接打不开怎么办 苹果上装的迅雷打不开怎么办 mp4进水水干了后打不开怎么办 迅雷闪退ios10.3怎么办 用辣椒辣到眼睛怎么办 辣椒辣到眼皮了怎么办 眼被辣椒辣了怎么办 老公欠银行的钱怎么办 离婚老公欠的钱怎么办 欠了几十万债怎么办 执行死刑时已怀孕怎么办 执行死刑世已经怀孕怎么办 孩子被爸爸打该怎么办 二年级的孩子逃课怎么办 龙妈和雪诺怎么办 车本扣6分怎么办 超速50扣了12分怎么办 开车超速扣12分怎么办 超速一次记12分怎么办? 驾照超速扣12分怎么办 c牌一次扣12分怎么办 超速罚500扣12分怎么办 高速超速记3分怎么办 高速超速记12分怎么办 过江隧道关闭了怎么办 高速限速60超了怎么办 超速扣了12分怎么办 上海 12分扣完了怎么办 狗抓伤只淌白沫怎么办 刑事案件法院迟迟不判决怎么办 法院判决书下来了没钱赔偿怎么办 被起诉开庭不去怎么办 地道塌陷造成房屋倒塌怎么办 海外看不了b站怎么办 最近脸胖了好多怎么办 最近胖了好多怎么办啊? 很瘦的人手胖怎么办 长得丑不敢相亲怎么办 abs抱死发不动了怎么办 qq飞车手游车到期了改装怎么办