{"id":2147,"date":"2021-05-21T20:54:39","date_gmt":"2021-05-21T12:54:39","guid":{"rendered":"https:\/\/coderbee.net\/?p=2147"},"modified":"2021-05-22T16:27:26","modified_gmt":"2021-05-22T08:27:26","slug":"futuretask-jdk1-8","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20210521\/2147","title":{"rendered":"FutureTask  (JDK1.8)"},"content":{"rendered":"<p>\u672c\u6587\u57fa\u4e8e JDK 1.8.0_101 \u5bf9\u5e94\u7684\u6e90\u7801\u3002<\/p>\n<p><code>FutureTask<\/code> \u8868\u793a\u4e00\u4e2a\u53ef\u53d6\u6d88\u7684\u5f02\u6b65\u8ba1\u7b97\u7684\u4efb\u52a1\uff0c\u5176\u6784\u9020\u5668\u63a5\u6536\u4e00\u4e2a <code>Runnable\/Callable<\/code> \u8868\u793a\u5f02\u6b65\u8ba1\u7b97\u7684\u4efb\u52a1\u3002\u867d\u7136\u8bf4 <code>FutureTask<\/code> \u652f\u6301\u53d6\u6d88\uff0c\u4f46\u4efb\u52a1\u4e00\u65e6\u5f00\u59cb\u6267\u884c\uff0c\u6267\u884c\u8fc7\u7a0b\u5c31\u53ef\u80fd\u4e0d\u53d7\u53d6\u6d88\u64cd\u4f5c\u7684\u5f71\u54cd\u3002<\/p>\n<p>\u5f53\u4efb\u52a1\u6ca1\u6709\u6267\u884c\u7ed3\u675f\u65f6\uff0c\u6240\u6709\u83b7\u53d6\u7ed3\u679c\u7684\u64cd\u4f5c\u90fd\u4f1a\u963b\u585e\u7b49\u5f85\u3002<\/p>\n<p>\u4ee5\u6808\u7684\u65b9\u5f0f\u7ef4\u62a4\u7b49\u5f85\u83b7\u53d6\u7ed3\u679c\u7684\u7ebf\u7a0b\uff0c\u56e0\u4e3a\u4e0d\u7528\u8003\u8651\u516c\u5e73\u6027\u7684\u95ee\u9898\uff0c\u7528\u6808\u66f4\u7b80\u5355\u3002<\/p>\n<p>FutureTask \u5728 JDK 1.7 \u7684\u65f6\u5019\u662f\u57fa\u4e8e AQS \u5b9e\u73b0\u7684\uff0c\u53ef\u4ee5\u770b<a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20140311\/844\">\u8fd9\u91cc<\/a>\u3002<\/p>\n<p>\u603b\u4f53\u4e0a\uff1a<\/p>\n<ul>\n<li><code>volatile int state<\/code> \u63a7\u5236\u6267\u884c\u72b6\u6001\uff0c<\/li>\n<li><code>volatile Thread runner<\/code> \u9632\u6b62\u91cd\u590d\u6267\u884c\uff0c<\/li>\n<li><code>volatile WaitNode waiters<\/code> \u4ee5\u6808\u5f62\u5f0f\u7ef4\u62a4\u7b49\u5f85\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<pre><code class=\"java\">\/\/ \u8981\u5f02\u6b65\u6267\u884c\u7684\u4efb\u52a1\uff0c\u6267\u884c\u5b8c\u540e\u7f6e\u4e3a null\nprivate Callable&lt;V&gt; callable;\n\n\/\/ \u6267\u884c\u7684\u7ed3\u679c\u6216\u6267\u884c\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\nprivate Object outcome; \/\/ \u975e volatile, \u901a\u8fc7\u8bfb\u5199 state \u5b57\u6bb5\u6765\u4fdd\u62a4\n\n\/\/ \u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b\uff0c\u5728 run \u65b9\u6cd5\u91cc\u901a\u8fc7 CAS \u6765\u8bbe\u7f6e\u3002\n\/\/ \u8fd8\u53ef\u4ee5\u7528\u6765\u9632\u6b62\u91cd\u590d\u6267\u884c\nprivate volatile Thread runner;\n\n\/\/ \u7ef4\u62a4\u7b49\u5f85\u7ebf\u7a0b\u7684\u6808\nprivate volatile WaitNode waiters;\n\n\/\/ \u4efb\u52a1\u7684\u6267\u884c\u72b6\u6001\nprivate volatile int state;\n\npublic FutureTask(Callable&lt;V&gt; callable) {\n    if (callable == null)\n        throw new NullPointerException();\n    this.callable = callable;\n    this.state = NEW;       \/\/ ensure visibility of callable\n}\n\npublic FutureTask(Runnable runnable, V result) {\n    this.callable = Executors.callable(runnable, result);\n    this.state = NEW;       \/\/ ensure visibility of callable\n}\n<\/code><\/pre>\n<h1>1. FutureTask \u7684\u72b6\u6001\u5b9a\u4e49<\/h1>\n<pre><code>private volatile int state;\nprivate static final int NEW          = 0; \/\/ \u53ef\u80fd\u672a\u6267\u884c\uff0c\u4e5f\u53ef\u80fd\u6267\u884c\u5b8c\u6210\u3001\u4f46\u6ca1\u8fd8\u6709\u6539\u53d8\u72b6\u6001\nprivate static final int COMPLETING   = 1; \/\/ \u5b8c\u6210\u4e2d\uff0c\u4ee5\u4e0b\u7684\u90fd\u8868\u793a\u4efb\u52a1\u7ec8\u6001\nprivate static final int NORMAL       = 2;\nprivate static final int EXCEPTIONAL  = 3;\nprivate static final int CANCELLED    = 4;\nprivate static final int INTERRUPTING = 5;\nprivate static final int INTERRUPTED  = 6;\n<\/code><\/pre>\n<p>\u72b6\u6001\u7684\u53d8\u5316\u53ef\u80fd\u6709\u5982\u4e0b\u7684\u8def\u5f84\uff1a<\/p>\n<pre><code>NEW -&gt; COMPLETING -&gt; NORMAL         \/\/ \u6b63\u5e38\u6267\u884c\u5b8c\u6210\nNEW -&gt; COMPLETING -&gt; EXCEPTIONAL    \/\/ \u6267\u884c\u629b\u51fa\u4e86\u5f02\u5e38\nNEW -&gt; CANCELLED                    \/\/ \nNEW -&gt; INTERRUPTING -&gt; INTERRUPTED  \/\/ \n<\/code><\/pre>\n<p><code>COMPLETING<\/code> \u72b6\u6001\u662f\u4e3a\u4e86\u7528\u6765\u4fdd\u62a4\u8bbe\u7f6e <code>outcome<\/code> \u5b57\u6bb5\u7684\u4e2d\u95f4\u72b6\u6001\u3001\u975e\u5e38\u77ed\u6682\u3002<\/p>\n<h1>2. \u6267\u884c\u4efb\u52a1<\/h1>\n<p>\u6267\u884c\u4efb\u52a1\u7684\u524d\u63d0\u662f\u72b6\u6001\u4e3a NEW\u3001\u4e14\u6267\u884c\u7ebf\u7a0b\u4e3a\u7a7a\u3002<\/p>\n<p>\u901a\u8fc7\u5bf9 runner \u5b57\u6bb5\u7684 CAS \u8bbe\u7f6e\u6765\u9632\u6b62\u88ab\u91cd\u590d\u6267\u884c\u3002<\/p>\n<p>\u4efb\u52a1\u4e0d\u7ba1\u662f\u6b63\u5e38\u6267\u884c\u5b8c\u8fd8\u662f\u6267\u884c\u8fc7\u7a0b\u4e2d\u629b\u51fa\u4e86\u5f02\u5e38\uff0c\u90fd\u8981\u5524\u9192\u6240\u6709\u7b49\u5f85\u83b7\u53d6\u7ed3\u679c\u7684\u7ebf\u7a0b\u3002<\/p>\n<p>\u4ece\u4e0b\u9762\u4ee3\u7801 <code>#1<\/code> \u6ce8\u91ca\u7684\u524d\u540e\u53ef\u77e5\uff0cNEW \u72b6\u6001\u6709\u53ef\u80fd\u662f\u4efb\u52a1\u8fd8\u6ca1\u5f00\u59cb\u6267\u884c\uff0c\u4e5f\u53ef\u80fd\u662f\u6267\u884c\u5b8c\u6210\u4e86\u3001\u4f46\u8fd8\u6ca1\u66f4\u65b0\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"java\">public void run() {\n    if (state != NEW ||\n        !UNSAFE.compareAndSwapObject(this, runnerOffset,\n                                     null, Thread.currentThread())) \/\/ \u9632\u6b62\u91cd\u590d\u6267\u884c\n        return;\n    try {\n        Callable&lt;V&gt; c = callable;\n        if (c != null &amp;&amp; state == NEW) { \/\/ \u72b6\u6001\u4ecd\u7136\u662f NEW\n            V result;\n            boolean ran;\n            try {\n                result = c.call(); \/\/ #1\n                ran = true;\n            } catch (Throwable ex) {\n                result = null;\n                ran = false;\n                setException(ex);\n            }\n            if (ran)\n                set(result);\n        }\n    } finally {\n        \/\/ runner must be non-null until state is settled to\n        \/\/ prevent concurrent calls to run()\n        \/\/ runner \u5fc5\u987b\u4fdd\u6301\u975e\u7a7a\u76f4\u81f3 state \u8bbe\u7f6e\u4e3a\u7ec8\u6001\u6765\u9632\u6b62\u5e76\u53d1\u8c03\u7528 run \u65b9\u6cd5\n        runner = null;\n        \/\/ state must be re-read after nulling runner to prevent\n        \/\/ leaked interrupts\n        int s = state;\n        if (s &gt;= INTERRUPTING)\n            handlePossibleCancellationInterrupt(s);\n    }\n}\n\nprotected void setException(Throwable t) {\n    \/\/ \u975e NEW \u72b6\u6001\uff0c\u8bf4\u660e\u88ab\u53d6\u6d88\u4e86\uff0c\u5ffd\u7565\u7ed3\u679c\n    if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {\n        outcome = t;\n        UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); \/\/ final state\n        finishCompletion();\n    }\n}\n\nprotected void set(V v) {\n    \/\/ \u975e NEW \u72b6\u6001\uff0c\u8bf4\u660e\u88ab\u53d6\u6d88\u4e86\uff0c\u5ffd\u7565\u7ed3\u679c\n    if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {\n        outcome = v;\n        UNSAFE.putOrderedInt(this, stateOffset, NORMAL); \/\/ \u7ec8\u6001\n        finishCompletion();             \/\/ \u5524\u9192\u6240\u6709\u7b49\u5f85\u83b7\u53d6\u7ed3\u679c\u7684\u7ebf\u7a0b\n    }\n}\n<\/code><\/pre>\n<h3>2.0 \u7ed3\u679c\u5904\u7406<\/h3>\n<p>\u8fd4\u56de\u7ed3\u679c\u6700\u7ec8\u662f\u6839\u636e\u72b6\u6001\u6765\u51b3\u5b9a\u7684\u3002\u54ea\u6015\u4efb\u52a1\u6267\u884c\u5b8c\u6210\u3001\u4f46\u7531\u4e8e\u88ab\u53d6\u6d88\u6210\u529f\uff0c\u4e5f\u4e0d\u4f1a\u8fd4\u56de\u6267\u884c\u7684\u7ed3\u679c\u3002<\/p>\n<pre><code>private V report(int s) throws ExecutionException {\n    Object x = outcome;\n    if (s == NORMAL)\n        return (V)x;\n    if (s &gt;= CANCELLED)\n        throw new CancellationException();\n    throw new ExecutionException((Throwable)x);\n}\n<\/code><\/pre>\n<h3>2.1 \u5524\u9192\u6240\u6709\u7b49\u5f85\u7ebf\u7a0b<\/h3>\n<p>\u8be5\u65b9\u6cd5\u5b9e\u73b0\u7684\u4eae\u70b9\u662f\uff1a\u628a <code>waiters<\/code> \u7684\u5f15\u7528\u4fdd\u5b58\u5230\u65b9\u6cd5\u672c\u5730\u53d8\u91cf\uff0c\u7136\u540e\u628a <code>waiters<\/code> \u8bbe\u7f6e\u4e3a <code>null<\/code>\uff0c\u8fd9\u6837\u5c31\u53ea\u6709\u8fd9\u4e2a\u65b9\u6cd5\u80fd\u8bbf\u95ee\u5230\u90a3\u4e2a\u6808\uff0c\u540e\u7eed\u7684\u5524\u9192\u8fc7\u7a0b\u5c31\u4e0d\u7528\u8003\u8651\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<pre><code class=\"java\">private void finishCompletion() {\n    \/\/ assert state &gt; COMPLETING;\n    \/\/ \u4e4b\u6240\u4ee5\u8981\u5faa\u73af\u662f\u53ef\u80fd\u5728\u91ca\u653e\u6808\u4e0a\u7ebf\u7a0b\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53c8\u6709\u65b0\u7684\u8282\u70b9\u52a0\u5165\u7b49\u5f85\u3002\n    for (WaitNode q; (q = waiters) != null;) {\n\n        if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {\n        \/\/ \u628a waiters \u8bbe\u4e3a null\uff0c\u5c31\u53ea\u6709\u672c\u5730\u53d8\u91cf q \u6307\u5411\u90a3\u4e2a\u6808\uff0c\u53ef\u4ee5\u4e0d\u7528\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\n            for (;;) {\n                Thread t = q.thread;\n                if (t != null) {\n                    q.thread = null;\n                    LockSupport.unpark(t);\n                }\n                WaitNode next = q.next;\n                if (next == null)\n                    break;\n                q.next = null; \/\/ unlink to help gc\n                q = next;\n            }\n            break;\n        }\n    }\n\n    done(); \/\/ \u9884\u7559\u7684\u94a9\u5b50\u65b9\u6cd5\uff0c\u7ed9\u5b50\u7c7b\u5b9e\u73b0\n\n    callable = null;        \/\/ to reduce footprint\n}\n<\/code><\/pre>\n<h1>3. \u53d6\u6d88<\/h1>\n<p>\u5fc5\u987b\u662f <code>NEW<\/code> \u72b6\u6001\u7684\u624d\u80fd\u53d6\u6d88\u3002<\/p>\n<p>\u53d6\u6d88\u64cd\u4f5c\u6700\u591a\u662f\u5bf9\u6267\u884c\u7ebf\u7a0b <code>runner<\/code> \u8bbe\u7f6e\u4e2d\u65ad\u6807\u5fd7\u4f4d\uff0c\u5982\u679c\u4efb\u52a1 <code>callable<\/code> \u5df2\u7ecf\u5728\u6267\u884c\u4e2d\uff0c\u662f\u4e0d\u4f1a\u7ec8\u6b62\u7684\uff0c\u9664\u975e\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u5224\u65ad\u4e86\u4e2d\u65ad\u6807\u5fd7\u4f4d\u3002<\/p>\n<pre><code class=\"java\">public boolean cancel(boolean mayInterruptIfRunning) {\n    if (!(state == NEW &amp;&amp;\n          UNSAFE.compareAndSwapInt(this, stateOffset, NEW,\n              mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))\n        return false;\n    try {    \/\/ in case call to interrupt throws exception\n        if (mayInterruptIfRunning) {\n            try {\n                Thread t = runner;\n                if (t != null)\n                    t.interrupt();  \/\/ \u8bbe\u7f6e\u4e2d\u65ad\u6807\u5fd7\u4f4d\n            } finally { \/\/ final state\n                UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);\n            }\n        }\n    } finally {\n        finishCompletion();\n    }\n    return true;\n}\n<\/code><\/pre>\n<h1>4. \u83b7\u53d6\u7ed3\u679c<\/h1>\n<p>\u83b7\u53d6\u7ed3\u679c\u65f6\uff0c\u5982\u679c\u5df2\u5b8c\u6210\u5219\u76f4\u63a5\u83b7\u53d6\uff0c\u5426\u5219\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u7b49\u5f85\uff0c\u5176\u6838\u5fc3\u662f <code>awaitDone<\/code> \u65b9\u6cd5\u3002<\/p>\n<p><code>awaitDone<\/code> \u65b9\u6cd5\u91cc\u5bf9\u72b6\u6001\u7684\u5224\u65ad\u987a\u5e8f\u662f\u4fdd\u8bc1\u6b63\u786e\u6027\u7684\u524d\u63d0\uff1a<br \/>\n0. \u6bcf\u6b21\u5faa\u73af\u90fd\u91cd\u5199\u8bfb\u53d6\u6700\u65b0\u7684\u72b6\u6001\u81f3\u672c\u5730\u53d8\u91cf\uff0c\u7528\u4e8e\u540e\u7eed\u591a\u6b21\u5224\u65ad\u3002<br \/>\n1. \u5982\u679c\u4efb\u52a1\u5df2\u7ecf\u662f\u7ec8\u6001\u5219\u8fd4\u56de\u3002<br \/>\n2. \u5982\u679c\u4efb\u52a1 COMPLETING \u5219\u4e0d\u9700\u8981\u6302\u8d77\u7ebf\u7a0b\uff0c\u76f4\u63a5 busy loop \u7b49\u5f85\u6700\u7ec8\u72b6\u6001\u3002<br \/>\n3. \u5426\u5219\u5904\u4e8e NEW \u72b6\u6001\uff0c\u901a\u8fc7\u591a\u6b21\u5faa\u73af\u5b8c\u6210\u5982\u4e0b\uff1a<br \/>\n\u30003.1 \u5982\u679c\u7b49\u5f85\u8282\u70b9\u4e3a\u7a7a\uff0c\u5219\u65b0\u5efa\uff1b<br \/>\n\u30003.2 \u5982\u679c\u7b49\u5f85\u8282\u70b9\u8fd8\u6ca1\u5165\u6808\uff0c\u5219\u538b\u5165\u6808\u9876\uff1b<br \/>\n\u30003.3 \u6302\u8d77\u7ebf\u7a0b\u3001\u7b49\u5f85\u5524\u9192\u3002<\/p>\n<pre><code class=\"java\">public V get() throws InterruptedException, ExecutionException {\n    int s = state;\n    if (s &lt;= COMPLETING)\n        s = awaitDone(false, 0L);\n    return report(s);\n}\n\nprivate int awaitDone(boolean timed, long nanos) throws InterruptedException {\n    final long deadline = timed ? System.nanoTime() + nanos : 0L;\n    WaitNode q = null;\n    boolean queued = false;\n    for (;;) {\n        \/\/ \u5982\u679c\u7ebf\u7a0b\u5df2\u7ecf\u88ab\u4e2d\u65ad\uff0c\u5219\u629b\u51fa\u5f02\u5e38\n        if (Thread.interrupted()) {\n            removeWaiter(q);\n            throw new InterruptedException();\n        }\n\n        int s = state;  \/\/ \u6bcf\u6b21\u5faa\u73af\u90fd\u91cd\u5199\u8bfb\u53d6\u6700\u65b0\u7684\u72b6\u6001\n        if (s &gt; COMPLETING) {\n            \/\/ \u4efb\u52a1\u5df2\u7ecf\u662f\u7ec8\u6001\n            if (q != null)\n                q.thread = null;\n            return s;\n        }\n        else if (s == COMPLETING) \/\/ cannot time out yet\n            Thread.yield();\n            \/\/ \u4efb\u52a1\u5df2\u7ecf\u6267\u884c\u5b8c\u6210\u3001\u5728\u8bbe\u7f6e\u7ed3\u679c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e0d\u9700\u8981\u6302\u8d77\u7ebf\u7a0b\n\n        \/\/ \u4ee5\u4e0b\u903b\u8f91\u7684\u90fd\u662f NEW \u72b6\u6001\u4e0b\u7684\n        else if (q == null)\n            q = new WaitNode();\n        else if (!queued) {\n            q.next = waiters;\n            \/\/ \u628a\u5f53\u524d\u7ebf\u7a0b\u8282\u70b9\u653e\u5165\u6808\u9876\n            queued = UNSAFE.compareAndSwapObject(this, waitersOffset, waiters, q);\n        } else if (timed) {\n            nanos = deadline - System.nanoTime();\n            if (nanos &lt;= 0L) {\n                removeWaiter(q);\n                return state;\n            }\n            LockSupport.parkNanos(this, nanos);\n        }\n        else\n            LockSupport.park(this);\n    }\n}\n<\/code><\/pre>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong> \u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u57fa\u4e8e JDK 1.8.0_101 \u5bf9\u5e94\u7684\u6e90\u7801\u3002 FutureTask \u8868\u793a\u4e00 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20210521\/2147\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[353,110],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2147"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2147"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2147\/revisions"}],"predecessor-version":[{"id":2154,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2147\/revisions\/2154"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}