EventExecutor扩展NetEx中的EventExecutorGroup是一个好的设计吗?

时间:2019-02-01 09:15:33

标签: java netty

有两个类别的符号:

/**
 * The {@link EventExecutor} is a special {@link EventExecutorGroup} which comes
 * with some handy methods to see if a {@link Thread} is executed in a event loop.
 * Besides this, it also extends the {@link EventExecutorGroup} to allow for a generic
 * way to access methods.
 *
 */
public interface EventExecutor extends EventExecutorGroup {  

/**
 * The {@link EventExecutorGroup} is responsible for providing the {@link EventExecutor}'s to use
 * via its {@link #next()} method. Besides this, it is also responsible for handling their
 * life-cycle and allows shutting them down in a global fashion.
 *
 */
public interface EventExecutorGroup extends ScheduledExecutorService, Iterable<EventExecutor> {  

EventExecutorGroupEventExecutor的容器,并管理其生命周期。没关系,而且很容易理解。

我认为设计目的主要是重用一些方法定义,并且感觉不太自然。没有人会做Thread extends ThreadPool,对吗?(嗯。还有EventLoop扩展了EventLoopGroup ....)

为什么元素扩展其容器并将其自身视为特殊容器?

我想知道我是否错过了一些优势。

1 个答案:

答案 0 :(得分:1)

基本上和EventExecutor一样,EventExecutorGroup仅包含自身。此外,这还允许在需要通过EventExecutor的地方重新使用EventExecutorGroup

这在许多情况下都很方便,例如,它允许您创建一个Bootstrap并使用一个EventLoop作为其EventLoopGroup,从而确保处理所有I / O。通过同一线程。例如,当您构建代理时,这非常有用。