中的listeners_Netty源码学习(6)-- Netty中的异步处理
生活随笔
收集整理的這篇文章主要介紹了
中的listeners_Netty源码学习(6)-- Netty中的异步处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java中的Future:對于一個異步操作,可以暫時返回一個Future對象,然后去做別的事情。最后通過get方法拿到結果。如果get時異步操作還沒有完成,則進行阻塞狀態。
Netty對Future類對java類進行了擴展,不但對future有些接口進行了改寫,還對Future類增加了觀察者模式。
public interface Future<V> {boolean cancel(boolean mayInterruptIfRunning);boolean isCancelled();boolean isDone();V get() throws InterruptedException, ExecutionException;V get(long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException; }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();@Overrideboolean cancel(boolean mayInterruptIfRunning); }public interface GenericFutureListener<F extends Future<?>> extends EventListener {/*** Invoked when the operation associated with the {@link Future} has been completed.** @param future the source {@link Future} which called this callback*/void operationComplete(F future) throws Exception; }JDK所提供的Future只能通過手動的方式檢查結果,檢查結果這個操作是阻塞的。netty對channelFuture進行了增強,通過ChannelFutureListener以回調的方式來獲取執行結果,沒有檢查操作。
注意:ChannelFutureListener的operationComplete方法是由I/O線程執行的,因此注意的是不要再這里執行耗時操作,否則需要用另外的線程池執行。
總結
以上是生活随笔為你收集整理的中的listeners_Netty源码学习(6)-- Netty中的异步处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 未來用工新趨勢_数字化商业浪潮来袭 未来
- 下一篇: echarts无数据时显示无数据_钣金无