中的listeners_Netty源码学习(6)-- Netty中的异步处理
生活随笔
收集整理的這篇文章主要介紹了
中的listeners_Netty源码学习(6)-- Netty中的异步处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java中的Future:對(duì)于一個(gè)異步操作,可以暫時(shí)返回一個(gè)Future對(duì)象,然后去做別的事情。最后通過(guò)get方法拿到結(jié)果。如果get時(shí)異步操作還沒有完成,則進(jìn)行阻塞狀態(tài)。
Netty對(duì)Future類對(duì)java類進(jìn)行了擴(kuò)展,不但對(duì)future有些接口進(jìn)行了改寫,還對(duì)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只能通過(guò)手動(dòng)的方式檢查結(jié)果,檢查結(jié)果這個(gè)操作是阻塞的。netty對(duì)channelFuture進(jìn)行了增強(qiáng),通過(guò)ChannelFutureListener以回調(diào)的方式來(lái)獲取執(zhí)行結(jié)果,沒有檢查操作。
注意:ChannelFutureListener的operationComplete方法是由I/O線程執(zhí)行的,因此注意的是不要再這里執(zhí)行耗時(shí)操作,否則需要用另外的線程池執(zhí)行。
總結(jié)
以上是生活随笔為你收集整理的中的listeners_Netty源码学习(6)-- Netty中的异步处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 未來用工新趨勢_数字化商业浪潮来袭 未来
- 下一篇: echarts无数据时显示无数据_钣金无