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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Netty--Future和Promise

發布時間:2024/4/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Netty--Future和Promise 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

概述

JDK.Future

Netty.Future

ChannelFuture

Promise

繼承關系

源碼分析

AbstractFuture

CompleteFuture

DefaultPromise


概述

Future代表了異步操作的結果,Netty的Future對JDK的Future進行了擴展。

JDK.Future

僅定義了是否完成以及是否取消的狀態判斷以及get方法。

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; }

Netty.Future

增加了:

  • 結果狀態
  • 監聽器管理
  • sync
  • await
public interface Future<V> extends java.util.concurrent.Future<V> {//statusboolean isSuccess();boolean isCancellable();Throwable cause();//listenerFuture<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);//syncFuture<V> sync() throws InterruptedException;Future<V> syncUninterruptibly();//awaitFuture<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); }

ChannelFuture

增加了相關channel信息。

狀態信息:

public interface ChannelFuture extends Future<Void> {Channel channel();boolean isVoid(); }

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();@OverridePromise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);@OverridePromise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);@OverridePromise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);@OverridePromise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);@OverridePromise<V> await() throws InterruptedException;@OverridePromise<V> awaitUninterruptibly();@OverridePromise<V> sync() throws InterruptedException;@OverridePromise<V> syncUninterruptibly(); }

繼承關系

源碼分析

AbstractFuture<V>

get()方法調用await()方法。

public abstract class AbstractFuture<V> implements Future<V> {@Overridepublic V get() throws InterruptedException, ExecutionException {await();Throwable cause = cause();if (cause == null) {return getNow();}if (cause instanceof CancellationException) {throw (CancellationException) cause;}throw new ExecutionException(cause);}@Overridepublic V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {if (await(timeout, unit)) {Throwable cause = cause();if (cause == null) {return getNow();}if (cause instanceof CancellationException) {throw (CancellationException) cause;}throw new ExecutionException(cause);}throw new TimeoutException();} }

CompleteFuture<V>

代表一個Future已經完成。

內部包含一個EventExecutor,用于注冊事件監聽器

public abstract class CompleteFuture<V> extends AbstractFuture<V> {private final EventExecutor executor;protected CompleteFuture(EventExecutor executor) {this.executor = executor;}@Overridepublic Future<V> addListener(GenericFutureListener<? extends Future<? super V>> listener) {if (listener == null) {throw new NullPointerException("listener");}DefaultPromise.notifyListener(executor(), this, listener);return this;}@Overridepublic Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {if (listeners == null) {throw new NullPointerException("listeners");}for (GenericFutureListener<? extends Future<? super V>> l: listeners) {if (l == null) {break;}DefaultPromise.notifyListener(executor(), this, l);}return this;} }

DefaultPromise<V>

設置狀態內部使用了一個AtomicReferenceFieldUpdater

??? private static final AtomicReferenceFieldUpdater<DefaultPromise, Object> RESULT_UPDATER =
??????????? AtomicReferenceFieldUpdater.newUpdater(DefaultPromise.class, Object.class, "result");

??

? private boolean setValue0(Object objResult) {
??????? if (RESULT_UPDATER.compareAndSet(this, null, objResult) ||
??????????? RESULT_UPDATER.compareAndSet(this, UNCANCELLABLE, objResult)) {
??????????? if (checkNotifyWaiters()) {
??????????????? notifyListeners();
??????????? }
??????????? return true;
??????? }
??????? return false;
??? }

?

總結

以上是生活随笔為你收集整理的Netty--Future和Promise的全部內容,希望文章能夠幫你解決所遇到的問題。

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