{"id":48845,"date":"2024-12-02T11:23:03","date_gmt":"2024-12-02T03:23:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48845\/"},"modified":"2024-12-02T11:23:03","modified_gmt":"2024-12-02T03:23:03","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e4%ba%a4%e6%9b%bf%e6%89%93%e5%8d%b0-foobar-%e9%97%ae%e9%a2%98%ef%bc%9a%e4%bd%bf%e7%94%a8-volatile-%e5%8f%98%e9%87%8f%e5%8d%a1%e6%ad%bb%e7%9a%84%e5%8e%9f%e5%9b%a0%e6%98%af-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48845\/","title":{"rendered":"\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 FooBar \u95ee\u9898\uff1a\u4f7f\u7528 Volatile \u53d8\u91cf\u5361\u6b7b\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u6765\u907f\u514d\u5fd9\u7b49\u5f85\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 FooBar \u95ee\u9898\uff1a\u4f7f\u7528 Volatile \u53d8\u91cf\u5361\u6b7b\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u6765\u907f\u514d\u5fd9\u7b49\u5f85\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 FooBar \u95ee\u9898\uff1a\u4f7f\u7528 Volatile \u53d8\u91cf\u5361\u6b7b\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u6765\u907f\u514d\u5fd9\u7b49\u5f85\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/17318318946739a85671022.jpg\" class=\"aligncenter\" title=\"\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 FooBar \u95ee\u9898\uff1a\u4f7f\u7528 Volatile \u53d8\u91cf\u5361\u6b7b\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u6765\u907f\u514d\u5fd9\u7b49\u5f85\uff1f\u63d2\u56fe\" alt=\"\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 FooBar \u95ee\u9898\uff1a\u4f7f\u7528 Volatile \u53d8\u91cf\u5361\u6b7b\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u6765\u907f\u514d\u5fd9\u7b49\u5f85\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u9898\u76ee\u6c42\u52a9\uff1a\u4f7f\u7528 volatile \u53d8\u91cf\u9047\u963b<\/strong><\/p>\n<p>\u5728\u89e3\u51b3 \u201c\u4ea4\u66ff\u6253\u5370 foobar\u201d \u8fd9\u9053\u9898\u76ee\u65f6\uff0c\u9898\u4e3b\u4f7f\u7528 2 \u4e2a volatile \u53d8\u91cf\u6765\u63a7\u5236\u591a\u7ebf\u7a0b\u7684\u903b\u8f91\uff0c\u5373 flag1 \u548c flag2\u3002\u7136\u800c\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u603b\u4f1a\u5361\u6b7b\u5728 while \u5faa\u73af\u4e2d\u3002<\/p>\n<p>\u8fd9\u5e76\u975e volatile \u673a\u5236\u7684\u95ee\u9898\uff0c\u56e0\u4e3a volatile \u4f1a\u9632\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u4fdd\u8bc1\u53d8\u91cf\u5728\u4e0d\u540c\u7ebf\u7a0b\u95f4\u4fdd\u6301\u4e00\u81f4\u6027\u3002\u95ee\u9898\u53ef\u80fd\u51fa\u5728\u9898\u4e3b\u5bf9 volatile \u53d8\u91cf\u7684\u7406\u89e3\u548c\u4f7f\u7528\u65b9\u5f0f\u4e0a\u3002<\/p>\n<p>\u5728\u9898\u4e3b\u7684\u4ee3\u7801\u4e2d\uff0cwhile(flag1) \u548c while(flag2) \u65b9\u5f0f\u662f\u4e00\u79cd <strong>\u5fd9\u7b49\u5f85<\/strong>\uff0c\u5373\u7ebf\u7a0b\u4f1a\u6301\u7eed\u5730\u68c0\u67e5\u5bf9\u5e94\u6807\u5fd7\u4f4d\u662f\u5426\u4e3a false\uff0c\u5bfc\u81f4\u7ebf\u7a0b\u6301\u7eed\u5360\u7528 cpu \u8d44\u6e90\uff0c\u4ece\u800c\u9020\u6210\u5361\u6b7b\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u5fd9\u7b49\u5f85\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>\u6761\u4ef6\u53d8\u91cf\u548c\u9501<\/strong> \u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u534f\u8c03\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u7684\u4fee\u6539\u793a\u4f8b\uff1a<\/p>\n<pre>class FooBar {\n    private int n;\n    private boolean flag = false;\n    private Lock lock = new ReentrantLock();\n    private Condition condition = lock.newCondition();\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        lock.lock();\n        try {\n            for (int i = 0; i &lt; n; i++) {\n                while (flag) {\n                    condition.await();\n                }\n                \/\/ printFoo.run() outputs \"foo\". Do not change or remove this line.\n                printFoo.run();\n                flag = true;\n                condition.signal();\n            }\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        lock.lock();\n        try {\n            for (int i = 0; i &lt; n; i++) {\n                while (!flag) {\n                    condition.await();\n                }\n                \/\/ printBar.run() outputs \"bar\". Do not change or remove this line.\n                printBar.run();\n                flag = false;\n                condition.signal();\n            }\n        } finally {\n            lock.unlock();\n        }\n    }\n}<\/pre>\n<p>\u5728\u4fee\u6539\u540e\u7684\u4ee3\u7801\u4e2d\uff0c\u9501 lock \u548c\u6761\u4ef6\u53d8\u91cf condition \u88ab\u7528\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u534f\u8c03\u3002\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff08flag \u4e3a true\uff09\uff0c\u7ebf\u7a0b\u4f1a\u8c03\u7528 condition.await() \u65b9\u6cd5\u6765\u7b49\u5f85\uff0c\u5e76\u91ca\u653e\u9501\u3002\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff08flag \u4e3a false\uff09\uff0c\u7ebf\u7a0b\u4f1a\u8c03\u7528 condition.signal() \u65b9\u6cd5\u6765\u5524\u9192\u5176\u4ed6\u7b49\u5f85\u7684\u7ebf\u7a0b\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 FooBar \u95ee\u9898\uff1a\u4f7f\u7528 Volatile \u53d8\u91cf\u5361\u6b7b\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u6765\u907f\u514d\u5fd9\u7b49\u5f85\uff1f \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 FooBar \u95ee\u9898\uff1a\u4f7f\u7528 Volatile \u53d8\u91cf\u5361\u6b7b\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u6765\u907f\u514d\u5fd9\u7b49\u5f85\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 \u591a\u7ebf\u7a0b\u9898\u76ee\u6c42\u52a9\uff1a\u4f7f\u7528 volatile \u53d8\u91cf\u9047\u963b \u5728\u89e3\u51b3 \u201c\u4ea4\u66ff\u6253\u5370 foobar\u201d \u8fd9\u9053\u9898\u76ee\u65f6\uff0c\u9898\u4e3b\u4f7f\u7528 2 \u4e2a volatile \u53d8\u91cf\u6765\u63a7\u5236\u591a\u7ebf\u7a0b\u7684\u903b\u8f91\uff0c\u5373 flag1 \u548c flag2\u3002\u7136\u800c\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u603b\u4f1a\u5361\u6b7b\u5728 while \u5faa\u73af\u4e2d\u3002 \u8fd9\u5e76\u975e volatile \u673a\u5236\u7684\u95ee\u9898\uff0c\u56e0\u4e3a volatile \u4f1a\u9632\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u4fdd\u8bc1\u53d8\u91cf\u5728\u4e0d\u540c\u7ebf\u7a0b\u95f4\u4fdd\u6301\u4e00\u81f4\u6027\u3002\u95ee\u9898\u53ef\u80fd\u51fa\u5728\u9898\u4e3b\u5bf9 volatile \u53d8\u91cf\u7684\u7406\u89e3\u548c\u4f7f\u7528\u65b9\u5f0f\u4e0a\u3002 \u5728\u9898\u4e3b\u7684\u4ee3\u7801\u4e2d\uff0cwhile(flag1) \u548c while(flag2) \u65b9\u5f0f\u662f\u4e00\u79cd \u5fd9\u7b49\u5f85\uff0c\u5373\u7ebf\u7a0b\u4f1a\u6301\u7eed\u5730\u68c0\u67e5\u5bf9\u5e94\u6807\u5fd7\u4f4d\u662f\u5426\u4e3a false\uff0c\u5bfc\u81f4\u7ebf\u7a0b\u6301\u7eed\u5360\u7528 cpu \u8d44\u6e90\uff0c\u4ece\u800c\u9020\u6210\u5361\u6b7b\u3002 \u4e3a\u4e86\u907f\u514d\u5fd9\u7b49\u5f85\uff0c\u53ef\u4ee5\u4f7f\u7528 \u6761\u4ef6\u53d8\u91cf\u548c\u9501 \u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u534f\u8c03\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u548c\u9501\u7684\u4fee\u6539\u793a\u4f8b\uff1a class FooBar { private int n; private boolean flag = false; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48845","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48845"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48845\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}