{"id":631,"date":"2013-12-13T17:28:53","date_gmt":"2013-12-13T09:28:53","guid":{"rendered":"http:\/\/coderbee.net\/?p=631"},"modified":"2013-12-13T17:50:28","modified_gmt":"2013-12-13T09:50:28","slug":"juc-%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90-%e4%b8%89-abstractqueuedsynchronizer-%e5%85%b1%e4%ba%ab%e6%a8%a1%e5%bc%8f-%e4%b8%8e-countdownlatch","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20131213\/631","title":{"rendered":"JUC \u6e90\u7801\u5206\u6790 \u4e09 AbstractQueuedSynchronizer \u5171\u4eab\u6a21\u5f0f \u4e0e CountDownLatch"},"content":{"rendered":"<h2>\u5171\u4eab\u6a21\u5f0f<\/h2>\n<p>\u5171\u4eab\u6a21\u5f0f\u5141\u8bb8\u4e00\u7ec4\u7ebf\u7a0b\u83b7\u53d6\u540c\u4e00\u4e2a\u8bb8\u53ef\u3002\u4e3a\u5b9e\u73b0\u5171\u4eab\u6a21\u5f0f\u5b50\u7c7b\u9700\u8981\u5b9e\u73b0\u4e24\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>tryAcquireShared\uff1a\u8fd4\u56deint\u7c7b\u578b\u7684\u503c\uff0c\u5c0f\u4e8e0\u8868\u793a\u83b7\u53d6\u5931\u8d25\uff0c\u7b49\u4e8e0\u8868\u793a\u83b7\u53d6\u6210\u529f\u4f46\u4e0d\u5141\u8bb8\u540e\u7eed\u66f4\u591a\u7684\u83b7\u53d6\uff0c\u5927\u4e8e0\u8868\u793a\u83b7\u53d6\u6210\u529f\u4e14\u5141\u8bb8\u66f4\u591a\u7684\u540e\u7eed\u83b7\u53d6\u3002<\/li>\n<li>tryReleaseShared\uff1a\u8fd4\u56detrue\u8868\u793a\u91ca\u653e\u8bb8\u53ef\u6210\u529f\uff0c\u53ef\u4ee5\u5524\u9192\u7b49\u5f85\u7ebf\u7a0b\uff1bfalse\u8868\u793a\u5931\u8d25\uff0c\u4e0d\u5524\u9192\u7b49\u5f85\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<h3>\u5171\u4eab\u83b7\u53d6 acquireShared<\/h3>\n<pre><code class=\"java\">public final void acquireShared(int arg) {\n    if (tryAcquireShared(arg) &lt; 0)\n        doAcquireShared(arg);\n}\n\nprivate void doAcquireShared(int arg) {\n       \/\/ \u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\uff0c\u4e0d\u7ba1\u662f\u5171\u4eab\u6a21\u5f0f\u8fd8\u662f\u72ec\u5360\u6a21\u5f0f\uff0c\u90fd\u5171\u4eab\u540c\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u3002\n    final Node node = addWaiter(Node.SHARED);\n    boolean failed = true;\n    try {\n        boolean interrupted = false;\n        for (;;) {\n            final Node p = node.predecessor();\n            if (p == head) {\n                int r = tryAcquireShared(arg); \/\/ \u5c1d\u8bd5\u83b7\u53d6\uff0c\u8fd4\u56de\u503c\u8868\u793a\u662f\u5426\u5141\u8bb8\u83b7\u53d6\n                if (r &gt;= 0) {\n                   \/\/ \u83b7\u53d6\u6210\u529f\n                   \/\/ \u628a\u81ea\u5df1\u8bbe\u4e3a\u5934\u7ed3\u70b9\u5e76\u4f20\u9012\u53ef\u4ee5\u83b7\u53d6\u7684\u4fe1\u53f7\n                   \/\/ node \u628a\u81ea\u5df1\u8bbe\u4e3a\u5934\u7ed3\u70b9\u540e\uff0c\u5b83\u7684\u540e\u7ee7\u53d1\u73b0\u5b83\u7684\u524d\u9a71\u662f\u5934\u7ed3\u70b9\u4e86\uff0c\u5c31\u4f1a\u5c1d\u8bd5\u83b7\u53d6\u3002\n                    setHeadAndPropagate(node, r);\n                    p.next = null; \/\/ help GC\n                    if (interrupted)\n                        selfInterrupt();\n                    failed = false;\n                    return;\n                }\n            }\n            if (shouldParkAfterFailedAcquire(p, node) &amp;&amp;\n                parkAndCheckInterrupt())\n                interrupted = true;\n        }\n    } finally {\n        if (failed)\n            cancelAcquire(node);\n    }\n}\n\nprivate void setHeadAndPropagate(Node node, int propagate) {\n    Node h = head; \/\/ Record old head for check below\n    setHead(node);\n    \/*\n     * \u5c1d\u8bd5\u901a\u77e5\u961f\u5217\u91cc\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff0c\u5982\u679c\uff1a\n     *       \u8c03\u7528\u8005\u6307\u793a\u6216\u8005\u4e4b\u524d\u64cd\u4f5c\u8bb0\u5f55\u663e\u793a\u9700\u8981\u4f20\u9012\n     *       (\u6ce8\u610f\uff1a\u8fd9\u91cc\u5bf9waitStatus\u4f7f\u7528\u5355\u4e00\u68c0\u67e5\uff0c\u56e0\u4e3aPROPAGATE\u53ef\u80fd\u88ab\u8f6c\u6362\u5230SIGNAL)\n     *   \u5e76\u4e14\n     *       \u4e0b\u4e00\u4e2a\u7ed3\u70b9\u4ee5\u5171\u4eab\u6a21\u5f0f\u7b49\u5f85\u6216\u8005\u6211\u4eec\u6839\u672c\u5c31\u4e0d\u77e5\u9053\uff0c\u56e0\u4e3a\u5b83\u662f\u7a7a\u7684\u3002\n     *\n     * \u5728\u8fd9\u4e9b\u68c0\u67e5\u6709\u70b9\u4fdd\u5b88\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u5524\u9192\uff0c\u4f46\u53ea\u662f\u5728\u591a\u91cd\u7ade\u4e89acquires\/releases\u65f6\uff0c\n     * \u56e0\u6b64\uff0c\u5927\u591a\u6570\u90fd\u662f\u73b0\u5728\u6216\u4e0d\u4e45\u5c31\u9700\u8981\u901a\u77e5\u7684\u3002\n     *\/\n    if (propagate &gt; 0 || h == null || h.waitStatus &lt; 0) {\n        Node s = node.next;\n        if (s == null || s.isShared())\n            doReleaseShared();\n    }\n}\n\nprivate void setHead(Node node) {\n    head = node;\n    node.thread = null; \/\/ for GC\n    node.prev = null;\n}\n<\/code><\/pre>\n<p><!--more--><\/p>\n<h3>\u5171\u4eab\u91ca\u653e releaseShared<\/h3>\n<p>\u91ca\u653e\u5171\u4eab\u8bb8\u53ef\u7684\u65f6\u5019\uff0c\u6700\u91cd\u8981\u7684\u662f\u4fdd\u8bc1\u4f20\u9012\u5524\u9192\u3002<\/p>\n<pre><code class=\"java\">public final boolean releaseShared(int arg) {\n    if (tryReleaseShared(arg)) {\n        doReleaseShared();\n        return true ;\n    }\n    return false;\n}\n\n\/\/ \u91ca\u653e\u5171\u4eab\u7684\u6838\u5fc3\u65b9\u6cd5\nprivate void doReleaseShared() {\n       \/\/ \u8981\u786e\u4fddrelease\u4f20\u9012\uff0c\u5373\u4f7f\u6709\u5176\u4ed6\u6b63\u5728\u8fdb\u884c\u7684acquires\/releases\u3002\n       \/\/ \u8fd9\u4e2a\u8fc7\u7a0b\u7684\u4e00\u822c\u505a\u6cd5\u662f\u5c1d\u8bd5unpark head\u7684\u540e\u7ee7\uff0c\u5982\u679c\u5b83\uff08head\uff09\u9700\u8981\u4fe1\u53f7\u3002\n       \/\/ \u5982\u679chead\u4e0d\u9700\u8981\u4fe1\u53f7\uff0c\u628a\u72b6\u6001\u8bbe\u4e3aPROPAGATE\u6765\u786e\u4fdd\u4e00\u65e6release\uff0c\u4f20\u9012\u53ef\u4ee5\u7ee7\u7eed\u3002\n       \/\/ \u53e6\u5916\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u5faa\u73af\u91cc\u505a\u8fd9\u4e2a\uff0c\u4ee5\u514d\u6709\u65b0\u8282\u70b9\u6dfb\u52a0\u8fdb\u6765\u3002\n       \/\/ \u4e0d\u50cfunparkSuccessor\u7684\u5176\u4ed6\u4f7f\u7528\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053CAS\u91cd\u7f6e\u72b6\u6001\u5931\u8d25\u4e0e\u5426\uff0c\u5982\u679c\u5931\u8d25\uff0c\u5219\u91cd\u65b0\u68c0\u6d4b\u3002\n    for (;;) {\n        Node h = head;\n        if (h != null &amp;&amp; h != tail) {\n            int ws = h.waitStatus;\n            if (ws == Node.SIGNAL) { \/\/ \u5934\u7ed3\u70b9\n                if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0)) {\n                    continue;  \/\/ \u5934\u7ed3\u70b9\u72b6\u6001\u88ab\u6539\u53d8\uff0c\u9700\u8981\u91cd\u65b0\u68c0\u6d4b\u3002loop to recheck cases\n                }\n                \/\/ CAS\u6210\u529f\u8bf4\u660eh\u7ed3\u70b9\u9700\u8981\u901a\u77e5\u540e\u7ee7\uff0c\u5524\u9192\n                unparkSuccessor(h);\n            }\n            else if (ws == 0 &amp;&amp;\n                     !compareAndSetWaitStatus(h, 0, Node.PROPAGATE)) {\n                   \/\/ \u5982\u679c\u662f\u521d\u59cb\u5934\u7ed3\u70b9\uff0c\u628a\u5176\u72b6\u6001\u8bbe\u4e3aPROPAGATE\uff0c\u786e\u4fdd\u4f20\u9012\u7ee7\u7eed\n                   \/\/ \u72b6\u6001\u66f4\u6539\u5931\u8d25\uff0c\u9700\u8981\u518d\u6b21\u68c0\u6d4b\u3002\n                continue;                \/\/ loop on failed CAS\n            }\n        }\n        if (h == head) {  \/\/ loop if head changed\n             \/\/ \u5982\u679c\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u5934\u7ed3\u70b9\u88ab\u6539\u53d8\uff08\u53ef\u80fd\u65b0\u589e\u7ed3\u70b9\u6216\u8005\u4e00\u4e2a\u88ab\u5524\u9192\u7ebf\u7a0b\u628a\u81ea\u5df1\u8bbe\u4e3a\u5934\u7ed3\u70b9\u4e86\uff09\uff0c\u9700\u8981\u518d\u6b21\u68c0\u6d4b\u3002\n            break;\n        }\n    }\n}\n<\/code><\/pre>\n<h2>CountDownLatch<\/h2>\n<p>\u4f5c\u7528\uff1a\u5728\u5b8c\u6210\u4e00\u7ec4\u6b63\u5728\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684\u64cd\u4f5c\u4e4b\u524d\uff0cCountDownLatch\u5141\u8bb8\u4e00\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u4e00\u76f4\u7b49\u5f85\u3002CountDownLatch\u53ea\u963b\u585e\u4e00\u6b21\uff0c\u5012\u6570\u52300\u4e4b\u540e\uff0c\u8c03\u7528await\u65b9\u6cd5\u7684\u5c06\u76f4\u63a5\u901a\u8fc7\u3002<\/p>\n<p>\u770b\u770b\u5b9e\u73b0\u963b\u585e\u4e0e\u653e\u884c\u7684\u5185\u90e8\u7c7b\uff1a<\/p>\n<pre><code class=\"java\">private static final class Sync extends AbstractQueuedSynchronizer {\n    private static final long serialVersionUID = 4982264981922014374L;\n\n    Sync( int count) {\n        setState(count); \/\/ \u9700\u8981countDown\u7684\u6b21\u6570\n    }\n\n    int getCount() {\n        return getState();\n    }\n\n    protected int tryAcquireShared( int acquires) {\n       \/\/ \u5982\u679c\u5012\u6570\u52300\uff0c\u8fd4\u56de1\u8868\u793a\u5141\u8bb8\u540e\u7eed\u83b7\u53d6\uff0c\u8fd9\u6837\u53ef\u4ee5\u8ba9AQS\u6846\u67b6\u901a\u77e5\u540e\u7ee7\uff0c\n       \/\/ \u5426\u5219\u8fd4\u56de-1\u8868\u793a\u5931\u8d25\uff0c\u4e0d\u80fd\u83b7\u53d6\uff0c\u7ebf\u7a0b\u4f1a\u8fdb\u5165\u7b49\u5f85\u3002\n        return (getState() == 0) ? 1 : -1;\n    }\n\n    protected boolean tryReleaseShared( int releases) {\n        \/\/ Decrement count; signal when transition to zero\n        for (;;) { \/\/ CountDownLatch.countDown()\u53ef\u80fd\u88ab\u591a\u7ebf\u7a0b\u8c03\u7528\uff0c\u9700\u8981\u5931\u8d25\u540e\u91cd\u8bd5\n            int c = getState();\n            if (c == 0)\n                return false ;\n\n            \/\/ \u6ce8\u610f\uff1a\u8fd9\u91cc\u662f\u51cf1\uff0c\u800c\u4e0d\u662f\u51cf\u53bbreleases\uff0c\u56e0\u4e3aCountDownLatch\u662f\u5bf9countDown\u8c03\u7528\u6b21\u6570\u7684\u8ba1\u6570\n            int nextc = c-1;\n            if (compareAndSetState(c, nextc))\n                return nextc == 0;\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u4ee3\u7801\u5f88\u7b80\u6d01\uff0c\u56e0\u4e3aAQS\u6846\u67b6\u786e\u5b9e\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n<p>CountDownLatch\u7c7b\u7684\u4e00\u4e9b\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">public CountDownLatch( int count) {\n    if (count &lt; 0) throw new IllegalArgumentException( \"count &lt; 0\");\n    this.sync = new Sync(count);\n}\n\npublic void await() throws InterruptedException {\n    sync.acquireSharedInterruptibly(1);\n}\n\npublic boolean await( long timeout, TimeUnit unit)\n    throws InterruptedException {\n    return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));\n}\n\npublic void countDown() {\n    sync.releaseShared(1);\n}\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><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>\u5171\u4eab\u6a21\u5f0f \u5171\u4eab\u6a21\u5f0f\u5141\u8bb8\u4e00\u7ec4\u7ebf\u7a0b\u83b7\u53d6\u540c\u4e00\u4e2a\u8bb8\u53ef\u3002\u4e3a\u5b9e\u73b0\u5171\u4eab\u6a21\u5f0f\u5b50\u7c7b\u9700\u8981\u5b9e\u73b0\u4e24\u4e2a\u65b9\u6cd5 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20131213\/631\">\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":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[122,129,231,110],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/631"}],"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=631"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"predecessor-version":[{"id":633,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/631\/revisions\/633"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}