日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

中的listeners_Netty源码学习(6)-- Netty中的异步处理

發布時間:2025/3/20 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 中的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中的异步处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。