简体   繁体   中英

Is it a good design that EventExecutor extends EventExecutorGroup in netty?

There are symbols of the two classes:

/**
 * 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> {  

The EventExecutorGroup is the container of the EventExecutor and manage their life-cycle. It's ok and easy to understand.

I think the design purpose is mainly to reuse some method definitions and feel not so natural. No one would make Thread extends ThreadPool , right?(emmmm.. There is EventLoop extends EventLoopGroup too....)

Why an element extends its container and treated itself as a special container?

I want to know if there are some advantages that I have missed.

It's basically as and EventExecutor is just EventExecutorGroup that only contains itself. Also this allows to re-use an EventExecutor where you are required to pass and EventExecutorGroup .

This comes in handy in many different situations like for example it allows you to create a Bootstrap and use one EventLoop as its EventLoopGroup and so ensure all the I/O is handled by the same thread. This is super useful for example when you build a proxy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM