{"id":3925,"date":"2025-07-20T10:44:39","date_gmt":"2025-07-20T02:44:39","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3925"},"modified":"2025-07-20T10:44:39","modified_gmt":"2025-07-20T02:44:39","slug":"%e5%8f%af%e9%87%8d%e5%85%a5%e9%94%81%e5%9c%a8-aqs-%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/20\/%e5%8f%af%e9%87%8d%e5%85%a5%e9%94%81%e5%9c%a8-aqs-%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%ef%bc%9f\/","title":{"rendered":"\u53ef\u91cd\u5165\u9501\u5728 AQS \u4e2d\u5982\u4f55\u5b9e\u73b0\uff1f"},"content":{"rendered":"\n<p><strong>\u53ef\u91cd\u5165\u9501\uff08ReentrantLock\uff09\u5728 AQS\uff08AbstractQueuedSynchronizer\uff09\u4e2d\u7684\u5b9e\u73b0<\/strong>\u662f Java \u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u6838\u5fc3\u673a\u5236\u4e4b\u4e00\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u4ece\u5b9e\u9645\u7ed3\u6784\u3001\u53ef\u91cd\u5165\u539f\u7406\u3001\u6838\u5fc3\u65b9\u6cd5\u3001\u6e90\u7801\u7247\u6bb5\u3001\u5185\u5b58\u8bed\u4e49\u7b49\u591a\u4e2a\u5c42\u9762\uff0c\u6df1\u5165\u5256\u6790\uff1a<\/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\u80cc\u666f\u77e5\u8bc6\u7b80\u8ff0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ec0\u4e48\u662f AQS\uff1f<\/h3>\n\n\n\n<p>AQS\uff0c\u5168\u79f0 <strong>AbstractQueuedSynchronizer<\/strong>\uff0c\u662f J.U.C\uff08<code>java.util.concurrent<\/code>\uff09\u5305\u4e2d\u7684\u57fa\u7840\u540c\u6b65\u6846\u67b6\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5b83\u57fa\u4e8e\u4e00\u4e2a volatile \u7684 int \u72b6\u6001\u53d8\u91cf\uff08<code>state<\/code>\uff09\u548c\u4e00\u4e2a FIFO \u961f\u5217<\/strong>\uff0c\u5b9e\u73b0\u5404\u79cd\u9501\u4e0e\u540c\u6b65\u5668\u7684\u57fa\u7840\uff0c\u6bd4\u5982\uff1a<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ReentrantLock<\/code><\/li>\n\n\n\n<li><code>CountDownLatch<\/code><\/li>\n\n\n\n<li><code>Semaphore<\/code><\/li>\n\n\n\n<li><code>ReentrantReadWriteLock<\/code><\/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\">\u4e8c\u3001\u4ec0\u4e48\u662f\u53ef\u91cd\u5165\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u53ef\u91cd\u5165\uff08Reentrancy\uff09\u662f\u6307\uff1a<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u540c\u4e00\u7ebf\u7a0b\u53ef\u4ee5<strong>\u91cd\u590d\u83b7\u53d6\u540c\u4e00\u628a\u9501<\/strong>\uff0c\u4e0d\u88ab\u963b\u585e\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lock.lock();   \/\/ \u7b2c\u4e00\u6b21\u52a0\u9501\nlock.lock();   \/\/ \u7b2c\u4e8c\u6b21\u52a0\u9501\uff1a\u4e0d\u963b\u585e\n...\nlock.unlock(); \/\/ \u91ca\u653e\u4e00\u6b21\nlock.unlock(); \/\/ \u771f\u6b63\u91ca\u653e<\/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\u53ef\u91cd\u5165\u9501\u7684\u5173\u952e\u70b9\uff08ReentrantLock \u4f9d\u8d56 AQS\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5173\u952e\u70b9<\/th><th>\u5b9e\u73b0\u7ec6\u8282<\/th><\/tr><\/thead><tbody><tr><td>\u9501\u72b6\u6001\u8ba1\u6570<\/td><td><code>state<\/code> \u8868\u793a\u9501\u7684\u6301\u6709\u72b6\u6001\uff08\u52a0\u9501\u6b21\u6570\uff09<\/td><\/tr><tr><td>\u9501\u6301\u6709\u8005\u8bb0\u5f55<\/td><td><code>exclusiveOwnerThread<\/code> \u8868\u793a\u5f53\u524d\u6301\u6709\u9501\u7684\u7ebf\u7a0b<\/td><\/tr><tr><td>\u53ef\u91cd\u5165\u5224\u65ad<\/td><td>\u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u518d\u6b21\u83b7\u53d6\u9501\uff0c\u5141\u8bb8\u52a0\u9501\u3001<code>state++<\/code><\/td><\/tr><tr><td>\u89e3\u9501\u673a\u5236<\/td><td>\u6bcf\u6b21 <code>unlock()<\/code>\uff0c<code>state--<\/code>\uff0c\u76f4\u5230 <code>state==0<\/code> \u624d\u91ca\u653e\u9501<\/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\">\u56db\u3001\u6e90\u7801\u6df1\u5ea6\u89e3\u6790\uff1a\u5982\u4f55\u5b9e\u73b0\u53ef\u91cd\u5165<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6e90\u7801\u4f4d\u7f6e\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ReentrantLock.java<\/code>\uff08\u5c01\u88c5\u63a5\u53e3\uff09<\/li>\n\n\n\n<li><code>AbstractQueuedSynchronizer.java<\/code>\uff08\u6838\u5fc3\u903b\u8f91\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. lock() \u52a0\u9501\u903b\u8f91\uff1a\u53ef\u91cd\u5165\u5224\u65ad<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public final void acquire(int arg) {\n    if (!tryAcquire(arg) &amp;&amp;\n        acquireQueued(addWaiter(Node.EXCLUSIVE), arg))\n        selfInterrupt();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. tryAcquire() \u5b9e\u73b0\u53ef\u91cd\u5165\u903b\u8f91<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>protected final boolean tryAcquire(int acquires) {\n    final Thread current = Thread.currentThread();\n    int c = getState();\n    if (c == 0) {\n        \/\/ \u5982\u679c\u6ca1\u4eba\u6301\u6709\u9501\uff0c\u5c1d\u8bd5\u83b7\u53d6\uff08\u7528 CAS\uff09\n        if (compareAndSetState(0, acquires)) {\n            setExclusiveOwnerThread(current);\n            return true;\n        }\n    } else if (current == getExclusiveOwnerThread()) {\n        \/\/ \ud83d\udd01 \u8fd9\u91cc\u662f\u201c\u53ef\u91cd\u5165\u201d\u7684\u5173\u952e\uff01\n        int nextc = c + acquires;\n        if (nextc &lt; 0) \/\/ overflow\n            throw new Error(\"Maximum lock count exceeded\");\n        setState(nextc); \/\/ \u589e\u52a0\u6301\u6709\u6b21\u6570\n        return true;\n    }\n    return false;\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u540c\u4e00\u4e2a\u7ebf\u7a0b\u518d\u6b21\u8fdb\u6765\uff0c\u4e0d\u518d\u6392\u961f\uff0c\u76f4\u63a5\u7d2f\u52a0 <code>state<\/code>\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. unlock() \u89e3\u9501\u903b\u8f91\uff1a\u9010\u5c42\u91ca\u653e<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public final boolean release(int arg) {\n    if (tryRelease(arg)) {\n        Node h = head;\n        if (h != null &amp;&amp; h.waitStatus != 0)\n            unparkSuccessor(h);\n        return true;\n    }\n    return false;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. tryRelease()\uff1astate&#8211;\uff0c\u53ea\u6709\u4e3a0\u624d\u771f\u6b63\u91ca\u653e\u9501<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>protected final boolean tryRelease(int releases) {\n    int c = getState() - releases;\n    if (Thread.currentThread() != getExclusiveOwnerThread())\n        throw new IllegalMonitorStateException();\n    boolean free = false;\n    if (c == 0) {\n        free = true;\n        setExclusiveOwnerThread(null);\n    }\n    setState(c); \/\/ \ud83d\udd01 \u53ef\u91cd\u5165\u5bf9\u5e94\u7684 state \u6b21\u6570\n    return free;\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\">\u4e94\u3001\u7ebf\u7a0b\u8c03\u5ea6\u6a21\u578b\u56fe\u89e3<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Thread-A    \u2500\u2500 lock() \u2192 state=1\n            \u2500\u2500 lock() \u2192 state=2\uff08\u53ef\u91cd\u5165\uff09\n\nThread-A    \u2500\u2500 unlock() \u2192 state=1\n            \u2500\u2500 unlock() \u2192 state=0 \u2192 \u5524\u9192\u4e0b\u4e00\u4e2a\u7ebf\u7a0b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5e95\u5c42\u6838\u5fc3\u673a\u5236<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u673a\u5236<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>CAS<\/td><td>\u6bd4\u8f83\u5e76\u4ea4\u6362 <code>state<\/code>\uff0c\u539f\u5b50\u66f4\u65b0\u72b6\u6001<\/td><\/tr><tr><td>volatile<\/td><td><code>state<\/code> \u662f volatile\uff0c\u786e\u4fdd\u591a\u7ebf\u7a0b\u4e0b\u7684\u53ef\u89c1\u6027<\/td><\/tr><tr><td>\u81ea\u65cb\u961f\u5217<\/td><td>AQS \u4e2d\u7684\u7b49\u5f85\u7ebf\u7a0b\u7528\u53cc\u5411\u94fe\u8868\uff08CLH \u961f\u5217\uff09\u7ba1\u7406<\/td><\/tr><tr><td>\u5185\u5b58\u8bed\u4e49<\/td><td><code>acquire<\/code> \u548c <code>release<\/code> \u5c01\u88c5\u4e86\u5185\u5b58\u5c4f\u969c<\/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\">\u4e03\u3001\u6e90\u7801\u4e0e\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd17 <a href=\"https:\/\/github.com\/openjdk\/jdk\/blob\/master\/src\/java.base\/share\/classes\/java\/util\/concurrent\/locks\/ReentrantLock.java\">ReentrantLock \u6e90\u7801\uff08GitHub &#8211; OpenJDK\uff09<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/github.com\/openjdk\/jdk\/blob\/master\/src\/java.base\/share\/classes\/java\/util\/concurrent\/locks\/AbstractQueuedSynchronizer.java\">AQS \u6e90\u7801\uff08GitHub\uff09<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 \u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b\uff1a\u7b2c 14 \u7ae0<\/li>\n\n\n\n<li>\ud83d\udcd8 \u300a\u6df1\u5165\u7406\u89e3 Java \u865a\u62df\u673a\u300b\uff1a\u7ebf\u7a0b\u5b89\u5168\u548c\u9501\u4f18\u5316<\/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\u53e5\u8bdd<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>AQS \u4e2d\u53ef\u91cd\u5165\u9501\u901a\u8fc7\uff1a<code>state<\/code> \u8ba1\u6570 + \u5f53\u524d\u7ebf\u7a0b\u5224\u65ad + CAS \u539f\u5b50\u64cd\u4f5c \u5b9e\u73b0\u7ebf\u7a0b\u7684\u591a\u6b21\u52a0\u9501\u548c\u5bf9\u5e94\u89e3\u9501\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>\u53ef\u91cd\u5165\u9501\uff08ReentrantLock\uff09\u5728 AQS\uff08AbstractQueuedSynchronizer\uff09\u4e2d\u7684\u5b9e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3926,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,86],"tags":[],"class_list":["post-3925","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\/3925","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=3925"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3925\/revisions"}],"predecessor-version":[{"id":3927,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3925\/revisions\/3927"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3926"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}