1.概述
我们前面进行过分析,channel为netty网络操作的抽象类,EventLoop负责处理注册到其上的Channel处理的I/O事件;EventLoopGroup是一个EventLoop的分组,它可以获取到一个或者多个的EventLoop对象。
2.类关系图
NioEventLoopGroup的类继承图,蓝色部分为对应的java类,绿色的部分就为一些接口的信息
3.EventExecutorGroup
io.netty.util.concurrent.EventExecutorGroup这个接口继承于ScheduledExecutorService, Iterable这两个接口。我们主要看下它进行自定义了哪些属于它自己的接口信息
//是否已经停止 boolean isShuttingDown();//关闭Future<?> shutdownGracefully();Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit);// 获取一个 EventExecutor 对象EventExecutor next();
还有一点需要进行注意,就是返回值的Future需要进行注意不是进行返回的是java.util.concurrent.Future而是 io.netty.util.concurrent.Future
4.AbstractEventExecutorGroup
io.netty.util.concurrent.AbstractEventExecutorGroup实现EventExecutorGroup接口。
4.1 submit
提交一个普通任务到EventExecutor中。
@Overridepublic Future<?> submit(Runnable task) {return next().submit(task);}@Overridepublic <T> Future<T> submit(Runnable task, T result) {return next().submit(task, result);}@Overridepublic <T> Future<T> submit(Callable<T> task) {return next().submit(task);}
4.2 schedule
提交一个定时任务到 EventExecutor 中
@Overridepublic ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {return next().schedule(command, delay, unit);}@Overridepublic <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {return next().schedule(callable, delay, unit);}@Overridepublic ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {return next().scheduleAtFixedRate(command, initialDelay, period, unit);}@Overridepublic ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {return next().scheduleWithFixedDelay(command, initialDelay, delay, unit);}
4.3 shutdown
@Overridepublic Future<?> shutdownGracefully() {return shutdownGracefully(DEFAULT_SHUTDOWN_QUIET_PERIOD, DEFAULT_SHUTDOWN_TIMEOUT, TimeUnit.SECONDS);}/*** @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} i