{"id":149520,"date":"2020-08-27T15:04:48","date_gmt":"2020-08-27T07:04:48","guid":{"rendered":"http:\/\/4563.org\/?p=149520"},"modified":"2020-08-27T15:04:48","modified_gmt":"2020-08-27T07:04:48","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-completablefuture-%e7%9a%84-thenapplyasync-%e6%b2%a1%e6%9c%89%e6%96%b0%e8%b5%b7%e4%b8%80%e4%b8%aa%e7%ba%bf%e7%a8%8b%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=149520","title":{"rendered":"\u4e3a\u4ec0\u4e48 CompletableFuture \u7684 thenApplyAsync \u6ca1\u6709\u65b0\u8d77\u4e00\u4e2a\u7ebf\u7a0b\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e3a\u4ec0\u4e48 CompletableFuture \u7684 thenApplyAsync \u6ca1\u6709\u65b0\u8d77\u4e00\u4e2a\u7ebf\u7a0b\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 18<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>    public static void test() {         CompletableFuture&lt;String&gt; completableFuture = CompletableFuture.supplyAsync(() -&gt; {             String supplyAsyncResult = \" \"+Thread.currentThread().getName()+\" Hello world! \";             try {                 Thread.sleep(1000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println(supplyAsyncResult);             return supplyAsyncResult;         }).thenApplyAsync(r -&gt; {  \/\/\u6dfb\u52a0\u540e\u7eed\u4efb\u52a1             String thenApplyResult = Thread.currentThread().getName()+r + \" thenApply! \";             System.out.println(thenApplyResult);             return thenApplyResult;         });          try {             System.out.println(completableFuture.get() + \" finish!\");         } catch (InterruptedException | ExecutionException e) {             e.printStackTrace();         }     } <\/code><\/pre>\n<p>\u6253\u5370\uff1a<\/p>\n<pre><code> ForkJoinPool.commonPool-worker-9 Hello world!  ForkJoinPool.commonPool-worker-9 ForkJoinPool.commonPool-worker-9 Hello world!  thenApply!  ForkJoinPool.commonPool-worker-9 ForkJoinPool.commonPool-worker-9 Hello world!  thenApply!  finish! <\/code><\/pre>\n<pre><code>        public void run() {             CompletableFuture&lt;T&gt; d; Supplier&lt;T&gt; f;             if ((d = dep) != null &amp;&amp; (f = fn) != null) {                 dep = null; fn = null;  \/\/\u53ea\u662f\u4e3a\u4e86\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\uff0c\u65b9\u4fbf GC                 if (d.result == null) {                     try {                         d.completeValue(f.get());  \/\/\u6267\u884c task                     } catch (Throwable ex) {       \/\/\u6267\u884c task \u671f\u95f4\u629b\u51fa\u4e86\u5f02\u5e38                         d.completeThrowable(ex);                     }                 }                 d.postComplete();             }         } <\/code><\/pre>\n<p>\u4ece\u6e90\u7801\u4e0a\u6765\u770b\uff0csupplyAsync \u65b0\u8d77\u4e86\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u7b49\u5230\u7ebf\u7a0b\u6267\u884c\u5b8c task\uff0c\u5f00\u59cb\u6267\u884c d.postComplete(),\u5373\u5f00\u59cb\u6267\u884c\u540e\u7eed task\uff0c\u7136\u540e postComplete \u4f1a\u6267\u884c\u540e\u7eed task \u7684 completion \u5bf9\u8c61\u7684 tryFire \u65b9\u6cd5\u3002<\/p>\n<pre><code>    static final class UniApply&lt;T,V&gt; extends UniCompletion&lt;T,V&gt; {         Function&lt;? super T,? extends V&gt; fn;         UniApply(Executor executor, CompletableFuture&lt;V&gt; dep,                  CompletableFuture&lt;T&gt; src,                  Function&lt;? super T,? extends V&gt; fn) {             super(executor, dep, src); this.fn = fn;         }         final CompletableFuture&lt;V&gt; tryFire(int mode) {             CompletableFuture&lt;V&gt; d; CompletableFuture&lt;T&gt; a;             if ((d = dep) == null ||                 !d.uniApply(a = src, fn, mode &gt; 0 ? null : this))\/\/\u8fd9\u91cc\u4f1a\u53d1\u73b0\u524d\u4e00\u4e2a stage \u6267\u884c\u5b8c\u6bd5\uff0c\u4f46\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u6c60                 return null;             dep = null; src = null; fn = null;             return d.postFire(a, mode);         }     } <\/code><\/pre>\n<pre><code>    final &lt;S&gt; boolean uniApply(CompletableFuture&lt;S&gt; a,                                Function&lt;? super S,? extends T&gt; f,                                UniApply&lt;S,T&gt; c) {         Object r; Throwable x;         if (a == null || (r = a.result) == null || f == null)             return false;         tryComplete: if (result == null) {             if (r instanceof AltResult) {                 if ((x = ((AltResult)r).ex) != null) {                     completeThrowable(x, r);                     break tryComplete;                 }                 r = null;             }             try {                 if (c != null &amp;&amp; !c.claim())\/\/\u4f1a\u6267\u884c\u5230\u8fd9\u91cc\uff0c\u7136\u540e\u53d1\u73b0 claim \u8fd4\u56de false                     return false;                 @SuppressWarnings(\"unchecked\") S s = (S) r;                 completeValue(f.apply(s));             } catch (Throwable ex) {                 completeThrowable(ex);             }         }         return true;     } <\/code><\/pre>\n<pre><code>        final boolean claim() {             Executor e = executor;             if (compareAndSetForkJoinTaskTag((short)0, (short)1)) {                 if (e == null)                     return true;                 executor = null; \/\/ disable                 e.execute(this); \/\/\u4f1a\u6267\u884c\u5230\u8fd9\u91cc\uff0c\u7136\u540e\u628a this completion \u5bf9\u8c61\u63d0\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u6267\u884c\uff0c\u5f53\u524d\u7ebf\u7a0b\u5373\u5c06\u8fd4\u56de             }             return false;         } <\/code><\/pre>\n<p>\u6211\u7684\u95ee\u9898\u5728\u4e8e\uff0c\u5f53 worker-9 \u7ebf\u7a0b\u6267\u884c\u5b8c\u7b2c\u4e00\u4e2a task \u4e4b\u540e\uff0c\u5b83\u628a\u7b2c\u4e8c\u4e2a task \u63d0\u4ea4\u7ed9\u4e86 executor \uff08<code>e.execute(this)<\/code>\uff09\uff0c\u7136\u540e\u7ebf\u7a0b\u5c31\u8fd4\u56de\u4e86\uff08\u4ece claim \u51fd\u6570\u4e00\u5c42\u4e00\u5c42\u8fd4\u56de\uff0c\u76f4\u5230\u8fd4\u56de postComplete \uff09\u3002\u90a3\u4e3a\u4ec0\u4e48\u7b2c\u4e8c\u4e2a task \u4ece\u6253\u5370\u7ed3\u679c\u6765\u770b\uff0c\u8fd8\u662f\u540c\u4e00\u4e2a worker-9 \u7ebf\u7a0b\u6765\u6267\u884c\u7684\uff1f<\/p>\n<p>\u8fd8\u662f\u8bf4\uff0c\u53ea\u662f\u56e0\u4e3a\u6211\u7684\u4f8b\u5b50\u6bd4\u8f83\u7b80\u5355\uff0c\u6240\u4ee5 executor \u6ca1\u6709\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u51fa\u6765\uff0c\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0cthenApplyAsync \u91cc\u9762\u5728\u6267\u884c<code>e.execute(this)<\/code>\u65f6\uff0c\u8fd8\u662f\u6709\u53ef\u80fd\u65b0\u8d77\u4e00\u4e2a\u7ebf\u7a0b\u7684\u5417\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3055645\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : passerbytiny <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             supplyAsync \u548c thenApplyAsync \u867d\u7136\u90fd\u662f\u5f02\u6b65\u8c03\u7528\uff0c\u4f46\u5b83\u4eec\u4e24\u4e2a\u4e4b\u95f4\u662f\u4e32\u884c\u7684\uff0c\u4e3a\u4ec0\u4e48\u5c31\u4e0d\u80fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\uff08\u6267\u884c\u5668\uff09\u4e2d\u88ab\u6267\u884c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3055646\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @passerbytiny <br \/>\u6ca1\u8bf4\u4e0d\u53ef\u4ee5\uff0c\u5b83\u4eec\u4e4b\u95f4\u80af\u5b9a\u662f\u4e32\u884c\u7684\uff0c\u4f46\u4e0d\u4e00\u5b9a\u662f\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5427\u3002\u4ece\u6e90\u7801\u4e0a\u53ef\u89c1\uff0csupplyAsync \u7684\u7ebf\u7a0b\u5e76\u4e0d\u662f\u76f4\u63a5\u6267\u884c\u4e0b\u4e00\u4e2a task \u7684\uff0c\u56e0\u4e3a\u5b83 e.execute(this)\u4e4b\u540e\u5c31\u9a6c\u4e0a\u8fd4\u56de\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3055647\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zyoo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             async \u7684\u8bed\u4e49\u662f\u4e0d\u4e00\u5b9a\u540c\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u6240\u4ee5\u8fd9\u4e2a\u53ea\u80fd\u8bf4\u662f\u5de7\u5408\u4e86\uff0c\u4f60\u53ef\u4ee5\u591a\u8bd5\u51e0\u628a\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3055648\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zyoo <br \/>\u591a\u8bd5\u51e0\u6b21\u4e5f\u4e00\u6837\u3002\u6211\u6000\u7591\u8fd9\u8ddf ForkJoinPool.commonPool()\u7684\u7ebf\u7a0b\u8c03\u5ea6\u6709\u5173\u7cfb\uff0c\u4f46\u6211\u73b0\u5728\u8fd8\u6ca1\u6765\u5f97\u53ca\u770b\u5b83\u7684\u539f\u7406\u5462\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3055649\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : passerbytiny <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @amiwrong123 \u5f02\u6b65\u4efb\u52a1\u90fd\u662f\u5c06\u4efb\u52a1\u63d0\u4ea4\u7ed9\u6267\u884c\u5668\u53bb\u6267\u884c\u7684\uff0c\u800c\u4e0d\u662f\u4ece\u7ebf\u7a0b\u6c60\u53d6\u51fa\u4e00\u4e2a\u7ebf\u7a0b\u7528\u6765\u6267\u884c\u4efb\u52a1\u3002\u9009\u62e9\u54ea\u4e2a\u7ebf\u7a0b\u662f\u7531\u6267\u884c\u5668\u81ea\u884c\u51b3\u5b9a\u7684\uff0c\u4efb\u52a1\u7684\u63d0\u4ea4\u8005\u5f88\u96be\u4e5f\u4e0d\u8be5\u5bf9\u7ebf\u7a0b\u9009\u62e9\u4ea7\u751f\u5f71\u54cd\u3002<\/p>\n<p>\u4ece\u9ad8\u5c42\u6b21\u4e0a\u770b\uff0cthenApplyAsync \u662f\u5728 applyAsync \u5b8c\u6210\u4e4b\u540e\u6267\u884c\u7684\uff0c\u6240\u4ee5\u6700\u4f18\u9009\u62e9\u5c31\u662f\u4e24\u8005\u4f7f\u7528\u540c\u4e00\u4e2a\u7ebf\u7a0b\uff08\u7ebf\u7a0b\u5524\u9192\u4e5f\u662f\u6709\u6210\u672c\u7684\uff09\u3002\u4ece\u6e90\u7801\u770b\uff0c\u4f60\u8981\u4e3b\u8981\u770b\u7684\u5e94\u8be5\u662f Executor \u7684\u6e90\u7801\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3055650\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @passerbytiny <br \/>\u597d\u5427\uff0c\u5927\u6982\u7406\u89e3\u4e86\u3002\u4e3b\u8981\u4e4b\u524d\u6211\u4ee5\u4e3a\u6211\u8fd9\u4e2a\u4f8b\u5b50\uff0capplyAsync \u548c thenApplyAsync \u7684\u6267\u884c\u7ebf\u7a0b\u80af\u5b9a\u662f\u540c\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4f46\u4ece\u6e90\u7801\u4e0a\u770b\u53d1\u73b0 \u524d\u4e00\u4e2a\u7ebf\u7a0b\u53ea\u662f\u63d0\u4ea4\u4efb\u52a1\u7ed9 Executor \u800c\u5df2\u3002<\/p>\n<p>\u6240\u4ee5\uff0capplyAsync \u548c thenApplyAsync \u7684\u6267\u884c\u7ebf\u7a0b\u4e0d\u4e00\u5b9a\u662f\u540c\u4e00\u4e2a\u5457\u3002\u53ea\u662f\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u7ebf\u7a0b\u6c60\u662f\u8fd9\u6837\u8c03\u5ea6\u7684\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3055651\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : tangzekk <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6728\u5b9d\u5389\u5bb3\u54e6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3055652\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : RedBeanIce <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @amiwrong123 #5<br \/>@passerbytiny #6<\/p>\n<p>JDK8<br \/>\u6c42\u95ee\u4e00\u4e0b\uff0c\u6211\u4e5f\u662f\u770b\u5230\u8fd9\u91cc\u5c06\u4efb\u52a1\u63d0\u4ea4\u5230 Executor <br \/>e.execute(new AsyncRun(d, f));<br \/>\u90a3\u4e48\u4e0b\u4e00\u6b65\u5e94\u8be5\u770b forkjoinpool \uff1f\u56e0\u4e3a\u9ed8\u8ba4\u662f\u4ed6\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48 CompletableFu&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149520"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149520"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149520\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149520"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}