NioEventLoop继承关系图
Future
netty包的Future
继承了JDK里的Future
,主要是增加了addListener()
和removeListener()
方法。
public interface Future<V> extends java.util.concurrent.Future<V> {
boolean isSuccess();
boolean isCancellable();
Throwable cause();
Future<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);
Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);
Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
Future<V> sync() throws InterruptedException;
Future<V> syncUninterruptibly();
Future<V> await() throws InterruptedException;
Future<V> awaitUninterruptibly();
boolean await(long timeout, TimeUnit unit) throws InterruptedException;
boolean await(long timeoutMillis) throws InterruptedException;
boolean awaitUninterruptibly(long timeout, TimeUnit unit);
boolean awaitUninterruptibly(long timeoutMillis);
V getNow();
@Override
boolean cancel(boolean mayInterruptIfRunning);
}
Promise
Promise
是一个可写的Future
。
public interface Promise<V> extends Future<V> {
Promise<V> setSuccess(V result);
boolean trySuccess(V result);
Promise<V> setFailure(Throwable cause);
boolean tryFailure(Throwable cause);
boolean setUncancellable();
@Override
Promise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);
@Override
Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
@Override
Promise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);
@Override
Promise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
@Override
Promise<V> await() throws InterruptedException;
@Override
Promise<V> awaitUninterruptibly();
@Override
Promise<V> sync() throws InterruptedException;
@Override
Promise<V> syncUninterruptibly();
}
EventExecutor
EventExecutor
主要是提供了inEventLoop()
方法查看一个线程是否在event loop中执行。
public interface EventExecutor extends EventExecutorGroup {
//返回this
@Override
EventExecutor next();
EventExecutorGroup parent();
boolean inEventLoop();
boolean inEventLoop(Thread thread);
<V> Promise<V> newPromise();
<V> ProgressivePromise<V> newProgressivePromise();
//这里的Future是netty包里面的,而不是jdk里面的那个
<V> Future<V> newSucceededFuture(V result);
<V> Future<V> newFailedFuture(Throwable cause);
}
AbstractEventExecutor
AbstractEventExecutor
没有实现具体的逻辑,主要是将实现代理给父类,或者抛出UnsupportedOperationException
异常。
AbstractScheduledEventExecutor
AbstractScheduledEventExecutor
实现了schedule()
方法,具体实现是将Runnable
或Callable
封装成ScheduledFutureTask
,然后加到字段PriorityQueue<ScheduledFutureTask<?>>
队列中。
队列中的ScheduledFutureTask是按照剩余时间大小排列的,AbstractEventExecutor
还提供了pollScheduledTask()
获取下一个到时的任务。
OrderedEventExecutor
OrderedEventExecutor
只是一个标记接口,没有任何方法,表示任务会按照顺序执行。
EventLoop
EventLoop
只是将父类parent()
方法返回,从EventExecutorGroup
,改为EventLoopGroup
。
public interface EventLoop extends OrderedEventExecutor, EventLoopGroup {
@Override
EventLoopGroup parent();
}
网友评论