{"id":3931,"date":"2025-07-20T10:51:52","date_gmt":"2025-07-20T02:51:52","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3931"},"modified":"2025-07-20T10:51:52","modified_gmt":"2025-07-20T02:51:52","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e7%94%a8-clh-%e9%98%9f%e5%88%97%ef%bc%9f%e5%ae%83%e7%9a%84%e4%bc%98%e5%8a%bf%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/20\/%e4%b8%ba%e4%bb%80%e4%b9%88%e7%94%a8-clh-%e9%98%9f%e5%88%97%ef%bc%9f%e5%ae%83%e7%9a%84%e4%bc%98%e5%8a%bf%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u7528 CLH \u961f\u5217\uff1f\u5b83\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"\n<p><code>CLH \u961f\u5217<\/code>\uff08<strong>Craig, Landin, and Hagersten Queue<\/strong>\uff09\u662f Java \u5e76\u53d1\u5305 <code>AbstractQueuedSynchronizer<\/code>\uff08AQS\uff09 \u7528\u6765\u5b9e\u73b0\u963b\u585e\u540c\u6b65\u5668\uff08\u5982 <code>ReentrantLock<\/code>\u3001<code>CountDownLatch<\/code> \u7b49\uff09\u7684\u6838\u5fc3<strong>\u7b49\u5f85\u961f\u5217\u6a21\u578b<\/strong>\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 CLH \u961f\u5217\uff1f<\/h2>\n\n\n\n<p><strong>CLH\uff08Craig\u2013Landin\u2013Hagersten\uff09\u961f\u5217<\/strong> \u662f\u4e00\u79cd <strong>\u57fa\u4e8e\u94fe\u8868\u7684\u81ea\u65cb\u9501\u7b49\u5f85\u961f\u5217<\/strong>\uff0c\u8bbe\u8ba1\u76ee\u7684\u662f\u4e3a\u4e86\u51cf\u5c11<strong>\u9501\u7ade\u4e89<\/strong>\u548c<strong>\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898<\/strong>\u3002<\/p>\n\n\n\n<p>AQS \u5728\u5b9e\u73b0\u540c\u6b65\u5668\u65f6\uff0c\u91c7\u7528\u7684\u662f\u4e00\u4e2a<strong>\u53d8\u79cd\u7684 CLH \u961f\u5217<\/strong>\uff0c\u7528\u4e8e\u7ba1\u7406\u6240\u6709\u7b49\u5f85\u83b7\u53d6\u9501\u7684\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001CLH \u961f\u5217\u7684\u7ed3\u6784\uff08AQS \u4e2d\u7684\u5b9e\u73b0\uff09<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u91c7\u7528 <strong>\u53cc\u5411\u94fe\u8868<\/strong>\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2a <strong>Node \u8282\u70b9<\/strong>\u3002<\/li>\n\n\n\n<li>\u5165\u961f\u987a\u5e8f\u4ee3\u8868\u52a0\u9501\u987a\u5e8f\uff0c\u7c7b\u4f3c FIFO\u3002<\/li>\n\n\n\n<li>\u5f53\u524d\u7ebf\u7a0b\u53ea\u5173\u5fc3\u5b83\u524d\u9a71\u8282\u70b9\u7684\u72b6\u6001\uff08\u5982\u662f\u5426\u88ab\u5524\u9192\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b80\u56fe\uff08\u7ebf\u7a0b\u7b49\u5f85\u961f\u5217\uff09\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+------+     +------+     +------+     +------+\n| head | --&gt; | T1   | --&gt; | T2   | --&gt; | T3   |\n+------+     +------+     +------+     +------+<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\u6bcf\u4e2a <code>Node<\/code> \u6709\u5982\u4e0b\u4e3b\u8981\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static final class Node {\n    volatile Node prev;    \/\/ \u524d\u9a71\u8282\u70b9\n    volatile Node next;    \/\/ \u540e\u7ee7\u8282\u70b9\n    volatile Thread thread; \/\/ \u5f53\u524d\u8282\u70b9\u5bf9\u5e94\u7ebf\u7a0b\n    volatile int waitStatus; \/\/ -1\u8868\u793aSIGNAL\uff0c\u4ee3\u8868\u524d\u9a71\u91ca\u653e\u65f6\u9700\u5524\u9192\u5b83\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\u3001AQS \u4e3a\u4ec0\u4e48\u91c7\u7528 CLH \u961f\u5217\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6838\u5fc3\u4f18\u52bf\u5982\u4e0b\uff1a<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f18\u52bf<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>1. \u975e\u963b\u585e\/\u51cf\u5c11\u9501\u7ade\u4e89<\/td><td>\u6bcf\u4e2a\u7ebf\u7a0b\u53ea<strong>\u5173\u6ce8\u524d\u9a71\u8282\u70b9\u72b6\u6001<\/strong>\uff0c\u4e0d\u9700\u904d\u5386\u6574\u4e2a\u961f\u5217\u6216\u62a2\u9501<\/td><\/tr><tr><td>2. \u9ad8\u6027\u80fd\u7684\u7f13\u5b58\u5c40\u90e8\u6027<\/td><td>\u6bcf\u4e2a\u7ebf\u7a0b\u81ea\u65cb\u5728\u672c\u5730\u53d8\u91cf\u4e0a\uff08\u6216\u5171\u4eab\u524d\u9a71\uff09\uff0c\u51cf\u5c11\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898<\/td><\/tr><tr><td>3. \u652f\u6301\u516c\u5e73\u6027\u961f\u5217\u7ba1\u7406<\/td><td>FIFO \u7ed3\u6784\u5929\u7136\u652f\u6301\u516c\u5e73\u52a0\u9501\uff08\u5982\u516c\u5e73\u9501\uff09<\/td><\/tr><tr><td>4. \u5185\u5b58\u5360\u7528\u4f4e<\/td><td>\u4e0d\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u521b\u5efa\u989d\u5916\u7684\u9501\u5bf9\u8c61\uff0c\u53ea\u7ef4\u62a4 Node \u94fe\u8868<\/td><\/tr><tr><td>5. \u7075\u6d3b\u6269\u5c55<\/td><td>\u53ef\u901a\u8fc7 <code>waitStatus<\/code> \u63a7\u5236\u4fe1\u53f7\u91cf\u3001\u8bfb\u5199\u9501\u3001\u6761\u4ef6\u53d8\u91cf\u7b49\u529f\u80fd<\/td><\/tr><tr><td>6. \u907f\u514d\u865a\u5047\u5524\u9192<\/td><td>\u8282\u70b9\u53ea\u5728\u771f\u6b63\u91ca\u653e\u9501\u540e\u88ab\u5524\u9192\uff0c\u903b\u8f91\u6e05\u6670\u4e14\u7cbe\u786e<\/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\u4e0e\u5176\u4ed6\u961f\u5217\u7684\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u961f\u5217\u6a21\u578b<\/th><th>\u7279\u70b9<\/th><th>AQS \u662f\u5426\u4f7f\u7528\uff1f<\/th><\/tr><\/thead><tbody><tr><td>CLH<\/td><td>\u524d\u9a71\u901a\u77e5\u81ea\u5df1\uff0c\u7ed3\u6784\u7d27\u51d1<\/td><td>\u2705 \u662f<\/td><\/tr><tr><td>MCS<\/td><td>\u524d\u9a71\u901a\u77e5\u540e\u7ee7\uff0c\u7ebf\u7a0b\u672c\u5730\u81ea\u65cb<\/td><td>\u274c \u5426\uff08\u9002\u5408 NUMA \u7cfb\u7edf\uff09<\/td><\/tr><tr><td>CAS-based<\/td><td>\u65e0\u961f\u5217\uff0c\u7ade\u4e89\u6fc0\u70c8\uff0c\u9002\u5408\u4f4e\u5e76\u53d1\u573a\u666f<\/td><td>\u274c \u5426<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>CLH \u6bd4 MCS \u66f4\u9002\u5408\u5171\u4eab\u7f13\u5b58\u7cfb\u7edf\uff08\u5982 JVM \u5185\u8fd0\u884c\u73af\u5883\uff09\uff0c\u539f\u56e0\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CLH \u8282\u70b9\u7684\u72b6\u6001\u5171\u4eab\u8f83\u5c11\uff0c\u7ebf\u7a0b\u53ea\u8bfb\u53d6\u524d\u9a71\u72b6\u6001\uff0c<strong>\u51cf\u5c11\u603b\u7ebf\u901a\u4fe1<\/strong>\u3002<\/li>\n\n\n\n<li>MCS \u8981\u901a\u77e5\u540e\u7ee7\uff0c<strong>\u5bb9\u6613\u5f15\u53d1 false sharing\uff08\u4f2a\u5171\u4eab\uff09\u95ee\u9898<\/strong>\u3002<\/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\">\u4e94\u3001\u6e90\u7801\u8bc1\u660e\uff08\u5165\u961f\u903b\u8f91\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ AQS.enqueue(Node node)\nprivate Node enq(final Node node) {\n    for (;;) {\n        Node t = tail;\n        if (t == null) { \/\/ \u961f\u5217\u4e3a\u7a7a\uff0c\u521d\u59cb\u5316\n            if (compareAndSetHead(new Node()))\n                tail = head;\n        } else {\n            node.prev = t;\n            if (compareAndSetTail(t, node)) {\n                t.next = node;\n                return t;\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5165\u961f\u64cd\u4f5c\u4f7f\u7528 CAS \u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c<strong>\u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u7ebf\u7a0b<\/strong>\uff0c\u8fd9\u6b63\u662f CLH \u7b49\u81ea\u65cb\u961f\u5217\u7684\u8bbe\u8ba1\u601d\u60f3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u56fe\u793a\u7406\u89e3 CLH \u7684\u81ea\u65cb\u884c\u4e3a<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>        +----------+      +----------+      +----------+\n head \u2192 |   Node   | --&gt;  | Thread A | --&gt;  | Thread B |\n        +----------+      +----------+      +----------+\n                                 \u2191\n                           Thread B \u81ea\u65cb\u5728 A \u7684\u72b6\u6001\u4e0a<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Thread B \u52a0\u5165\u961f\u5217\u65f6\u5173\u6ce8\u524d\u9a71 A\u3002<\/li>\n\n\n\n<li>Thread A \u91ca\u653e\u9501\u540e\u5524\u9192 B\u3002<\/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\">\u4e03\u3001\u5b9e\u9645\u6548\u679c\u5bf9\u6bd4\uff08\u6027\u80fd\u548c\u54cd\u5e94\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>CLH \u961f\u5217\u8868\u73b0<\/th><\/tr><\/thead><tbody><tr><td>\u4f4e\u4e89\u7528\u65f6\u5ef6\u8fdf<\/td><td>\u2b07\ufe0f \u6781\u4f4e\uff0c\u51e0\u4e4e\u65e0\u963b\u585e<\/td><\/tr><tr><td>\u9ad8\u5e76\u53d1<\/td><td>\u2b06\ufe0f \u9ad8\u6548\u4e14\u516c\u5e73<\/td><\/tr><tr><td>\u53ef\u6269\u5c55\u6027<\/td><td>\u2705 \u652f\u6301 Condition\u3001\u5171\u4eab\u9501\u3001\u5199\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\">\u516b\u3001\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b\u7b2c 14 \u7ae0\uff1a\u540c\u6b65\u5668\u6846\u67b6<\/li>\n\n\n\n<li>\u300a\u6df1\u5165\u7406\u89e3 Java \u865a\u62df\u673a\u300b\u7b2c 13 \u7ae0\uff1a\u540c\u6b65\u673a\u5236<\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/openjdk\/jdk\/blob\/master\/src\/java.base\/share\/classes\/java\/util\/concurrent\/locks\/AbstractQueuedSynchronizer.java\">OpenJDK AQS \u6e90\u7801\uff08GitHub\uff09<\/a><\/li>\n\n\n\n<li>CLH \u539f\u59cb\u8bba\u6587: &#8220;Scalable Queue-based Spin Locks with Timeout Support&#8221; \u2014 M. Scott et al.<\/li>\n\n\n\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/CLH_lock\">Wikipedia &#8211; CLH lock<\/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\u53e3\u8bc0<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>AQS \u7528 CLH\uff0c\u6392\u961f\u62a2\u9501\u4e0d\u62e5\u6324\uff1b<br>\u81ea\u65cb\u53ea\u770b\u524d\u8282\u70b9\uff0c\u6027\u80fd\u516c\u5e73\u53c8\u9ad8\u6548\uff01<\/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>CLH \u961f\u5217\uff08Craig, Landin, and Hagersten Queue\uff09\u662f Java \u5e76\u53d1\u5305 Ab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3932,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86,68],"tags":[],"class_list":["post-3931","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\/3931","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=3931"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3931\/revisions"}],"predecessor-version":[{"id":3933,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3931\/revisions\/3933"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3932"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}