{"id":3901,"date":"2025-07-13T11:54:51","date_gmt":"2025-07-13T03:54:51","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3901"},"modified":"2025-07-13T11:54:51","modified_gmt":"2025-07-13T03:54:51","slug":"%e7%ae%80%e8%bf%b0%e4%bc%aa%e5%85%b1%e4%ba%ab%e7%9a%84%e6%a6%82%e5%bf%b5%e4%bb%a5%e5%8f%8a%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/13\/%e7%ae%80%e8%bf%b0%e4%bc%aa%e5%85%b1%e4%ba%ab%e7%9a%84%e6%a6%82%e5%bf%b5%e4%bb%a5%e5%8f%8a%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d\/","title":{"rendered":"\u7b80\u8ff0\u4f2a\u5171\u4eab\u7684\u6982\u5ff5\u4ee5\u53ca\u5982\u4f55\u907f\u514d"},"content":{"rendered":"\n<p>\u4f2a\u5171\u4eab\uff08<strong>False Sharing<\/strong>\uff09\u662f<strong>\u591a\u7ebf\u7a0b\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u4e00\u79cd\u6027\u80fd\u9690\u60a3<\/strong>\uff0c\u5e38\u51fa\u73b0\u5728\u4f7f\u7528\u591a\u6838 CPU \u8fdb\u884c\u5171\u4eab\u5185\u5b58\u8bbf\u95ee\u65f6\u3002\u867d\u7136\u4e0d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u9519\u8bef\uff0c\u4f46\u4f1a<strong>\u4e25\u91cd\u5f71\u54cd\u6027\u80fd<\/strong>\uff0c\u5c24\u5176\u5728\u9ad8\u9891\u5199\u64cd\u4f5c\u4e0b\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\u4f2a\u5171\u4eab\uff08False Sharing\uff09\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9a\u4e49\uff1a<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u540c\u53d8\u91cf\uff0c\u4f46\u8fd9\u4e9b\u53d8\u91cf\u5171\u4eab\u4e86\u540c\u4e00\u4e2a\u7f13\u5b58\u884c\uff08cache line\uff09<\/strong>\uff0c\u4f1a\u5bfc\u81f4\u9891\u7e41\u7684\u7f13\u5b58\u540c\u6b65\uff0c\u4ea7\u751f\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u901a\u4fd7\u7406\u89e3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CPU \u7f13\u5b58\u662f\u4ee5 <strong>\u7f13\u5b58\u884c\uff08cache line\uff09<\/strong> \u4e3a\u5355\u4f4d\u8bfb\u5199\u7684\uff08\u901a\u5e38\u4e3a <strong>64 \u5b57\u8282<\/strong>\uff09\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u7684\u4e0d\u540c\u53d8\u91cf<strong>\u6070\u597d\u843d\u5728\u540c\u4e00\u4e2a cache line<\/strong> \u4e0a\uff1a<\/li>\n\n\n\n<li><strong>\u5373\u4f7f\u64cd\u4f5c\u4e0d\u540c\u53d8\u91cf\uff0c\u4ecd\u4f1a\u5bfc\u81f4\u7f13\u5b58\u5931\u6548<\/strong>\uff08cache invalidation\uff09\u3002<\/li>\n\n\n\n<li>\u591a\u4e2a CPU \u9891\u7e41\u5730\u8fdb\u884c\u7f13\u5b58\u4e00\u81f4\u6027\u540c\u6b65\uff08MESI \u534f\u8bae\uff09\uff0c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\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\">\u4e8c\u3001\u4e3a\u4ec0\u4e48\u4f1a\u6709\u4f2a\u5171\u4eab\u95ee\u9898\uff1f<\/h2>\n\n\n\n<p>Java \u4e2d\u4e00\u4e2a\u5bf9\u8c61\u5305\u542b\u591a\u4e2a\u5b57\u6bb5\u65f6\uff0c\u5b83\u4eec\u53ef\u80fd<strong>\u7d27\u5bc6\u6392\u5217<\/strong>\u5728\u5185\u5b58\u4e2d\uff0c\u751a\u81f3\u591a\u4e2a\u5bf9\u8c61\u4e5f\u53ef\u80fd\u5728\u6570\u7ec4\u91cc\u7d27\u90bb\u6392\u5e03\u3002<\/p>\n\n\n\n<p>\u5f53\u4e0d\u540c\u7ebf\u7a0b\u4fee\u6539\u8fd9\u4e9b\u5b57\u6bb5\uff08\u5373\u4fbf\u4e0d\u662f\u540c\u4e00\u4e2a\u5b57\u6bb5\uff09\uff0c<strong>\u53ea\u8981\u5b83\u4eec\u843d\u5728\u540c\u4e00\u7f13\u5b58\u884c<\/strong>\uff0c\u5c31\u4f1a\u5f15\u53d1\u4f2a\u5171\u4eab\u95ee\u9898\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u4f2a\u5171\u4eab\u793a\u4f8b\uff08Java\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class FalseSharingDemo {\n    static class Data {\n        public volatile long value = 0L;\n    }\n\n    static final int NUM_THREADS = 4;\n    static final long ITERATIONS = 10_000_000L;\n    static final Data&#91;] datas = new Data&#91;NUM_THREADS];\n\n    static {\n        for (int i = 0; i &lt; NUM_THREADS; i++) {\n            datas&#91;i] = new Data();\n        }\n    }\n\n    public static void main(String&#91;] args) throws InterruptedException {\n        Thread&#91;] threads = new Thread&#91;NUM_THREADS];\n        for (int i = 0; i &lt; NUM_THREADS; i++) {\n            final int index = i;\n            threads&#91;i] = new Thread(() -&gt; {\n                for (long j = 0; j &lt; ITERATIONS; j++) {\n                    datas&#91;index].value = j;\n                }\n            });\n        }\n\n        long start = System.nanoTime();\n        for (Thread t : threads) t.start();\n        for (Thread t : threads) t.join();\n        long end = System.nanoTime();\n\n        System.out.println(\"Duration: \" + (end - start) \/ 1_000_000 + \" ms\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udd3a <strong>\u95ee\u9898\uff1a<\/strong><br>\u6240\u6709 <code>value<\/code> \u5b57\u6bb5\u53ef\u80fd\u88ab\u5206\u914d\u5728\u540c\u4e00\u4e2a\u7f13\u5b58\u884c\u4e2d\uff0c\u591a\u7ebf\u7a0b\u540c\u65f6\u5199\u5165\u4e0d\u540c\u5143\u7d20\uff0c\u4f9d\u7136\u4f1a\u4e92\u76f8\u5e72\u6270\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\u5982\u4f55\u907f\u514d\u4f2a\u5171\u4eab\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 1\uff1a\u4f7f\u7528\u7f13\u5b58\u884c\u586b\u5145\uff08padding\uff09<\/h3>\n\n\n\n<p>\u624b\u52a8\u6dfb\u52a0\u65e0\u7528\u5b57\u6bb5\uff0c\u4f7f\u771f\u6b63\u8981\u7528\u7684\u5b57\u6bb5\u4e0d\u5171\u4eab\u540c\u4e00 cache line\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class PaddedData {\n    public volatile long value = 0L;\n    \/\/ \u6dfb\u52a0\u586b\u5145\u5b57\u6bb5\uff0c\u907f\u514d\u4f2a\u5171\u4eab\n    public long p1, p2, p3, p4, p5, p6, p7;\n}<\/code><\/pre>\n\n\n\n<p>\u6bcf\u4e2a\u5b57\u6bb5\u5360 8 \u5b57\u8282\uff0c\u6dfb\u52a0 7 \u4e2a\u5373\u53ef\u586b\u6ee1 64 \u5b57\u8282\u7f13\u5b58\u884c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 2\uff1a\u4f7f\u7528 JDK 8 \u7684 <code>@Contended<\/code> \u6ce8\u89e3\uff08\u63a8\u8350\u65b9\u5f0f\uff09<\/h3>\n\n\n\n<p>JDK 8+ \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u907f\u514d\u4f2a\u5171\u4eab\u7684\u6ce8\u89e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import jdk.internal.vm.annotation.Contended;\n\npublic class ContendedData {\n    @Contended\n    public volatile long value;\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u9700\u8981 JVM \u53c2\u6570\u5f00\u542f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-XX:-RestrictContended<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 3\uff1a\u4f7f\u7528\u6570\u7ec4\u5206\u914d\u95f4\u9694\u7a7a\u95f4\uff08\u4e0d\u63a8\u8350\uff09<\/h3>\n\n\n\n<p>\u4eba\u4e3a\u5728\u6570\u7ec4\u4e2d\u52a0\u95f4\u9694\u9879\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>datas&#91;i * 16] = new Data();<\/code><\/pre>\n\n\n\n<p>\u8ba9\u5b9e\u9645\u8bbf\u95ee\u9879\u8ddd\u79bb\u8db3\u591f\u8fdc\uff0c<strong>\u4f46\u8fd9\u79cd\u65b9\u5f0f\u4f9d\u8d56\u5177\u4f53\u5b9e\u73b0\uff0c\u4e0d\u53ef\u9760<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 4\uff1a\u4f7f\u7528\u9ad8\u6027\u80fd\u5e76\u53d1\u7c7b\uff08\u5982 LongAdder\uff09<\/h3>\n\n\n\n<p>JDK \u63d0\u4f9b\u4e86 <code>LongAdder<\/code> \u7b49\u4e13\u95e8\u4f18\u5316\u7684\u7c7b\uff0c\u5b83\u4eec<strong>\u5185\u90e8\u4f7f\u7528\u5206\u6bb5 + padding<\/strong>\u6280\u672f\u907f\u514d\u4f2a\u5171\u4eab\u3002<\/p>\n\n\n\n<p>\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u9891\u7e41\u52a0\u6cd5\u7edf\u8ba1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LongAdder adder = new LongAdder();\nadder.increment(); \/\/ \u6bd4 AtomicLong \u6027\u80fd\u9ad8<\/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\u4f2a\u5171\u4eab vs \u771f\u5171\u4eab<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u771f\u5171\u4eab<\/td><td>\u591a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u4e2a\u53d8\u91cf<\/td><\/tr><tr><td>\u4f2a\u5171\u4eab<\/td><td>\u591a\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u540c\u53d8\u91cf\uff0c\u4f46\u5728\u540c\u4e00 cache line \u5185<\/td><\/tr><tr><td>\u65e0\u5171\u4eab<\/td><td>\u591a\u7ebf\u7a0b\u5404\u81ea\u72ec\u7acb\u8bbf\u95ee\u5b8c\u5168\u4e0d\u540c\u7684 cache line \u6570\u636e<\/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\u53c2\u8003\u8d44\u6599\u4e0e\u5b98\u65b9\u51fa\u5904<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/openjdk.org\/jeps\/142\">OpenJDK @Contended \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/jcip.net\/\">\u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b<\/a> &#8211; Brian Goetz\uff08\u63a8\u8350\u4e66\u7c4d\uff09<\/li>\n\n\n\n<li><a href=\"https:\/\/mechanical-sympathy.blogspot.com\/2011\/07\/false-sharing.html\">Martin Thompson: False Sharing in Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/openjdk.org\/jeps\/142\">JEP 142: Eliminate False Sharing with @Contended<\/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\">\u4e03\u3001\u603b\u7ed3\uff08\u9762\u8bd5 &amp; \u5b9e\u6218\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u95ee\u9898<\/th><th>\u7b80\u6d01\u56de\u7b54<\/th><\/tr><\/thead><tbody><tr><td>\u4ec0\u4e48\u662f\u4f2a\u5171\u4eab\uff1f<\/td><td>\u591a\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u540c\u53d8\u91cf\uff0c\u4f46\u53d8\u91cf\u5171\u4eab\u540c\u4e00\u7f13\u5b58\u884c\uff0c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d<\/td><\/tr><tr><td>\u4e3a\u4ec0\u4e48\u4f1a\u6162\uff1f<\/td><td>\u7f13\u5b58\u4e00\u81f4\u6027\u534f\u8bae\u5bfc\u81f4\u9891\u7e41\u65e0\u6548\u540c\u6b65\uff08MESI \u534f\u8bae\uff09<\/td><\/tr><tr><td>\u600e\u4e48\u907f\u514d\uff1f<\/td><td>\u4f7f\u7528 padding\u3001<code>@Contended<\/code>\u3001\u6216\u9ad8\u5e76\u53d1\u7c7b\u5982 <code>LongAdder<\/code><\/td><\/tr><tr><td>\u5b9e\u9645\u5f71\u54cd\uff1f<\/td><td>\u5728\u9ad8\u5e76\u53d1\u5199\u64cd\u4f5c\u4e2d\uff0c\u6027\u80fd\u53ef\u80fd\u4e0b\u964d 5~30 \u500d<\/td><\/tr><\/tbody><\/table><\/figure>\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>\u4f2a\u5171\u4eab\uff08False Sharing\uff09\u662f\u591a\u7ebf\u7a0b\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u4e00\u79cd\u6027\u80fd\u9690\u60a3\uff0c\u5e38\u51fa\u73b0\u5728\u4f7f\u7528\u591a\u6838 CPU \u8fdb\u884c\u5171\u4eab\u5185\u5b58\u8bbf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3902,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-3901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3901","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=3901"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3901\/revisions"}],"predecessor-version":[{"id":3903,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3901\/revisions\/3903"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3902"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}