{"id":3934,"date":"2025-07-20T10:55:07","date_gmt":"2025-07-20T02:55:07","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3934"},"modified":"2025-07-20T10:55:07","modified_gmt":"2025-07-20T02:55:07","slug":"aqs-%e5%a6%82%e4%bd%95%e6%94%af%e6%8c%81-condition-%e7%9a%84%e7%ad%89%e5%be%85-%e5%94%a4%e9%86%92%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/20\/aqs-%e5%a6%82%e4%bd%95%e6%94%af%e6%8c%81-condition-%e7%9a%84%e7%ad%89%e5%be%85-%e5%94%a4%e9%86%92%ef%bc%9f\/","title":{"rendered":"AQS \u5982\u4f55\u652f\u6301 Condition \u7684\u7b49\u5f85\/\u5524\u9192\uff1f"},"content":{"rendered":"\n<p>\u5728 Java \u7684\u5e76\u53d1\u6846\u67b6\u4e2d\uff0c<code>AQS<\/code>\uff08<code>AbstractQueuedSynchronizer<\/code>\uff09\u901a\u8fc7\u5185\u90e8\u673a\u5236\u63d0\u4f9b\u4e86\u5bf9 <code>Condition<\/code> \u7684\u5f3a\u5927\u652f\u6301\uff0c\u7528\u4e8e\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684<strong>\u7b49\u5f85 \/ \u901a\u77e5<\/strong>\u534f\u8c03\u3002<\/p>\n\n\n\n<p>\u8fd9\u662f Java \u4e2d\u6784\u5efa <strong>\u53ef\u4e2d\u65ad\u3001\u53ef\u9650\u65f6\u3001\u53ef\u591a\u6761\u4ef6\u7b49\u5f85\u961f\u5217<\/strong>\u7684\u57fa\u7840\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4ec0\u4e48\u662f Condition\uff1f<\/h2>\n\n\n\n<p><code>Condition<\/code> \u662f <code>Lock<\/code> \u63a5\u53e3\u7684\u4e00\u4e2a\u5b50\u7cfb\u7edf\uff0c\u7528\u6765\u66ff\u4ee3 <code>Object.wait()\/notify()<\/code>\uff0c\u5e76\u7531 <code>AQS<\/code> \u63d0\u4f9b\u5e95\u5c42\u652f\u6301\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Lock lock = new ReentrantLock();\nCondition condition = lock.newCondition();<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6bcf\u4e00\u4e2a <code>Condition<\/code> \u90fd\u7ef4\u62a4\u7740\u4e00\u4e2a<strong>\u72ec\u7acb\u7684\u7b49\u5f85\u961f\u5217<\/strong>\uff0c\u7528\u4e8e\u5b58\u653e<strong>\u8c03\u7528 <code>await()<\/code> \u540e\u963b\u585e\u7684\u7ebf\u7a0b\u8282\u70b9<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001AQS \u4e2d Condition \u7684\u5173\u952e\u673a\u5236<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">AQS \u4e2d\uff0cCondition \u662f\u901a\u8fc7\u4e00\u4e2a\u5d4c\u5957\u7c7b\u5b9e\u73b0\u7684\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class AbstractQueuedSynchronizer {\n    public class ConditionObject implements Condition {\n        ...\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">AQS \u6838\u5fc3\u961f\u5217\u7ed3\u6784\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\u540c\u6b65\u961f\u5217\uff08Sync Queue\uff09      \u6761\u4ef6\u961f\u5217\uff08Condition Queue\uff09\n+------+                  +------+\n| head | --&gt; Node1       | NodeX | --&gt; NodeY --&gt; ...\n+------+                  +------+<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u7b49\u5f85\u4e0e\u5524\u9192\u8fc7\u7a0b\u7b80\u8ff0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. await() \u7b49\u5f85\u6d41\u7a0b\uff08ConditionObject.await\uff09<\/h3>\n\n\n\n<p>\u8c03\u7528 <code>condition.await()<\/code> \u4f1a\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5f53\u524d\u7ebf\u7a0b\u5c01\u88c5\u6210 Node \u52a0\u5165 condition queue\uff1b<\/li>\n\n\n\n<li>\u91ca\u653e\u9501\uff08<code>ReentrantLock.unlock()<\/code>\uff09\uff1b<\/li>\n\n\n\n<li>\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\uff08park\uff09\uff1b<\/li>\n\n\n\n<li>\u7b49\u5f85\u88ab <code>signal()<\/code> \u5524\u9192\u540e\uff0c\u91cd\u65b0\u5c1d\u8bd5\u7ade\u4e89\u9501\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6e90\u7801\u903b\u8f91\uff08\u7b80\u5316\uff09\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public final void await() throws InterruptedException {\n    if (Thread.interrupted()) throw new InterruptedException();\n\n    Node node = addConditionWaiter();   \/\/ \u52a0\u5165 Condition \u961f\u5217\n    int savedState = fullyRelease(node); \/\/ \u91ca\u653e\u9501\n    boolean interrupted = false;\n\n    while (!isOnSyncQueue(node)) {  \/\/ \u76f4\u5230\u8fdb\u5165\u540c\u6b65\u961f\u5217\n        LockSupport.park(this);     \/\/ \u963b\u585e\u7ebf\u7a0b\n        if (Thread.interrupted()) interrupted = true;\n    }\n\n    \/\/ \u88ab\u5524\u9192\u540e\u518d\u6b21\u52a0\u5165\u540c\u6b65\u961f\u5217\u5e76\u7ade\u4e89\u9501\n    acquireQueued(node, savedState);\n    if (interrupted) throw new InterruptedException();\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. signal() \u5524\u9192\u6d41\u7a0b\uff08ConditionObject.signal\uff09<\/h3>\n\n\n\n<p><code>signal()<\/code> \u4f1a\u5c06 Condition Queue \u4e2d<strong>\u6700\u524d\u9762\u7684\u8282\u70b9<\/strong>\u8f6c\u79fb\u5230 <strong>Sync Queue<\/strong> \u4e2d\uff0c\u7b49\u5f85\u91cd\u65b0\u83b7\u53d6\u9501\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6e90\u7801\u903b\u8f91\uff08\u7b80\u5316\uff09\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public final void signal() {\n    if (!isHeldExclusively()) throw new IllegalMonitorStateException();\n    Node first = firstWaiter;\n    if (first != null)\n        doSignal(first); \/\/ \u8f6c\u79fb\u5230\u540c\u6b65\u961f\u5217\n}\n\nprivate void doSignal(Node first) {\n    do {\n        if ((firstWaiter = first.nextWaiter) == null)\n            lastWaiter = null;\n        first.nextWaiter = null;\n    } while (!transferForSignal(first) &amp;&amp; (first = firstWaiter) != null);\n}<\/code><\/pre>\n\n\n\n<p><code>transferForSignal(Node node)<\/code> \u4f1a\u5c06\u8be5 Node \u7684\u72b6\u6001\u4fee\u6539\u5e76\u6dfb\u52a0\u5230 <code>AQS<\/code> \u7684\u540c\u6b65\u961f\u5217\uff08Sync Queue\uff09\u5c3e\u90e8\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6574\u4e2a\u6d41\u7a0b\u65f6\u5e8f\u56fe<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7ebf\u7a0bA:                  Condition Queue            Sync Queue\n\nlock.lock()\n  \u2514\u2500\u2500\u2192 condition.await()\u2500\u2500\u2510\n                          \u2193\n                    &#91;A - Node] \u2190\u2500\u2500\u2500\u2500\u2500\u2510\n                          \u2193         \u2502\n      lock.unlock() \u2192 park          \u2502\n                                    \u2502\n\u7ebf\u7a0bB:                              \u2502\n  \u2514\u2500\u2500\u2192 lock.lock()\n  \u2514\u2500\u2500\u2192 condition.signal() \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n                          \u2193         \u2193\n                    &#91;A - Node] \u2192 \u5165 Sync Queue\n                          \u2193\n                    unpark(Thread A)\n                          \u2193\n                    Thread A \u91cd\u65b0\u7ade\u4e89\u9501<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001Condition vs Object.wait()<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th><code>Condition<\/code><\/th><th><code>Object.wait()<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u591a\u6761\u4ef6\u961f\u5217<\/td><td>\u2705 \u652f\u6301\u591a\u4e2a <code>Condition<\/code><\/td><td>\u274c \u53ea\u80fd\u6709\u4e00\u4e2a wait set<\/td><\/tr><tr><td>\u7cbe\u7ec6\u901a\u77e5<\/td><td>\u2705 \u53ef\u4ee5 <code>signal()<\/code> \u6216 <code>signalAll()<\/code><\/td><td>\u274c \u53ea\u80fd <code>notify\/notifyAll<\/code><\/td><\/tr><tr><td>\u9501\u7c7b\u578b<\/td><td>\u914d\u5408 <code>Lock<\/code>\uff08\u5982 <code>ReentrantLock<\/code>\uff09\u4f7f\u7528<\/td><td>\u914d\u5408 <code>synchronized<\/code> \u4f7f\u7528<\/td><\/tr><tr><td>\u53ef\u4e2d\u65ad\/\u9650\u65f6<\/td><td>\u2705 <code>await()<\/code> \u53ef\u8bbe\u7f6e\u8d85\u65f6\u3001\u4e2d\u65ad<\/td><td>\u90e8\u5206\u652f\u6301<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5b9e\u6218\u6848\u4f8b\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class BoundedBuffer&lt;T&gt; {\n    private final Queue&lt;T&gt; queue = new LinkedList&lt;&gt;();\n    private final int capacity;\n\n    private final Lock lock = new ReentrantLock();\n    private final Condition notFull  = lock.newCondition();\n    private final Condition notEmpty = lock.newCondition();\n\n    public BoundedBuffer(int size) {\n        this.capacity = size;\n    }\n\n    public void put(T item) throws InterruptedException {\n        lock.lock();\n        try {\n            while (queue.size() == capacity) {\n                notFull.await();  \/\/ \u7b49\u5f85\u975e\u6ee1\n            }\n            queue.add(item);\n            notEmpty.signal();    \/\/ \u901a\u77e5\u6d88\u8d39\u8005\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public T take() throws InterruptedException {\n        lock.lock();\n        try {\n            while (queue.isEmpty()) {\n                notEmpty.await(); \/\/ \u7b49\u5f85\u975e\u7a7a\n            }\n            T item = queue.remove();\n            notFull.signal();     \/\/ \u901a\u77e5\u751f\u4ea7\u8005\n            return item;\n        } finally {\n            lock.unlock();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u6743\u5a01\u8d44\u6599\u63a8\u8350<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/openjdk\/jdk\/blob\/master\/src\/java.base\/share\/classes\/java\/util\/concurrent\/locks\/AbstractQueuedSynchronizer.java\">OpenJDK &#8211; AbstractQueuedSynchronizer \u6e90\u7801<\/a><\/li>\n\n\n\n<li><em>Java Concurrency in Practice<\/em>\uff08Java \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff09\u2014\u2014 \u7b2c 14 \u7ae0<\/li>\n\n\n\n<li><em>\u6df1\u5165\u7406\u89e3 Java \u865a\u62df\u673a<\/em> \u2014 \u9501\u673a\u5236\u90e8\u5206<\/li>\n\n\n\n<li>\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/locks\/Condition.html\">Java Condition \u63a5\u53e3<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3\u4e00\u56fe\u6d41\uff08\u8bb0\u5fc6\u53e3\u8bc0\uff09<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>await \u662f\u201c\u5165\u961f\u963b\u585e + \u91ca\u653e\u9501\u201d\uff0c signal \u662f\u201c\u79fb\u51fa + \u52a0\u5165 sync \u961f\u5217\u201d\uff0c \u5524\u9192\u540e\u8fd8\u8981\u62a2\u9501\uff0c\u624d\u80fd\u771f\u7684\u7ee7\u7eed\u8dd1\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u7684\u5e76\u53d1\u6846\u67b6\u4e2d\uff0cAQS\uff08AbstractQueuedSynchronizer\uff09\u901a\u8fc7\u5185\u90e8\u673a\u5236\u63d0\u4f9b\u4e86\u5bf9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3935,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,86],"tags":[],"class_list":["post-3934","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3934"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3934\/revisions"}],"predecessor-version":[{"id":3936,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3934\/revisions\/3936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3935"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}