您的位置:首页 > 其它

Netty2——EventExecutor

2016-04-08 13:26 344 查看


EventExecutorGroup接口:

public interface EventExecutorGroup extends ScheduledExecutorService, Iterable<EventExecutor> {


在ScheduleExecutorService和Iterable的基础上增加了以下接口:

boolean isShuttingDown();

Future<?> shutdownGracefully();

Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit);

Future<?> terminationFuture();

EventExecutor next();


EventExecutorGroup继承Iterable接口表明他是一个EventExecutor容器。

EventExecutor接口:

public interface EventExecutor extends EventExecutorGroup {


在EventExecutorGroup的基础上增加了以下接口:

EventExecutorGroup parent();

boolean inEventLoop();

boolean inEventLoop(Thread thread);

<V> Promise<V> newPromise();

<V> ProgressivePromise<V> newProgressivePromise();

<V> Future<V> newSucceededFuture(V result);

<V> Future<V> newFailedFuture(Throwable cause);


需要关注的是inEventLoop方法,用于判断当前线程或指定线程是否是EventLoop线程。

EventLoop的概念:http://www.ruanyifeng.com/blog/2013/10/event_loop.html。

另外虽然实现了EventExecutorGroup接口,但EventExecutor并不是容器。

AbstractEventExecutor抽象类:

public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {


这个类对一些通用方法做了实现。需要关注的是该类中包含一个私有的内部类EventExecutorIterator(迭代器)

private final class EventExecutorIterator implements Iterator<EventExecutor> {
private boolean nextCalled;

@Override
public boolean hasNext() {
return !nextCalled;
}

@Override
public EventExecutor next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
nextCalled = true;
return AbstractEventExecutor.this;
}

@Override
public void remove() {
throw new UnsupportedOperationException("read-only");
}
}


可以看到不管怎么遍历都智能拿到AbstractEventExecutor对象自己。这充分表明AbstractEventExecutor和其子类不是作为EventExecutor容器的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: