{"id":64998,"date":"2025-05-03T09:36:15","date_gmt":"2025-05-03T01:36:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64998\/"},"modified":"2025-05-03T09:36:15","modified_gmt":"2025-05-03T01:36:15","slug":"%e4%bd%bf%e7%94%a8-volatile-%e5%8f%98%e9%87%8f%e6%8e%a7%e5%88%b6%e5%a4%9a%e7%ba%bf%e7%a8%8b%e4%ba%a4%e6%9b%bf%e6%89%93%e5%8d%b0%e3%80%8cfoobar%e3%80%8d%e6%97%b6%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64998\/","title":{"rendered":"\u4f7f\u7528 volatile \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370\u300cFooBar\u300d\u65f6\u5982\u4f55\u907f\u514d\u6b7b\u9501\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173102801379075.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 volatile \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370\u300cFooBar\u300d\u65f6\u5982\u4f55\u907f\u514d\u6b7b\u9501\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 volatile \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370\u300cFooBar\u300d\u65f6\u5982\u4f55\u907f\u514d\u6b7b\u9501\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528\u4e24\u4e2a volatile \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u65f6\u6b7b\u9501\u95ee\u9898<\/strong><\/p>\n<p>\u9898\u76ee\u8981\u6c42\u8bbe\u8ba1\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4ea4\u66ff\u6253\u5370\u300cfoobar\u300d\u3002\u5f00\u53d1\u8005\u6309\u7167\u9898\u610f\u4f7f\u7528\u4e86\u4e24\u4e2a volatile \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u903b\u8f91\uff0c\u4f46\u8fd0\u884c\u540e\u7a0b\u5e8f\u5361\u6b7b\u5728 while \u5faa\u73af\u4e2d\u3002<\/p>\n<p><strong>\u539f\u56e0\u5206\u6790<\/strong><\/p>\n<p>\u4f7f\u7528 volatile \u53d8\u91cf\u786e\u5b9e\u53ef\u4ee5\u9632\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u4f46\u5b83\u4e0d\u80fd\u89e3\u51b3\u7ebf\u7a0b\u540c\u4e00\u7247\u6bb5\u5185\u5b58\u533a\u57df\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u5171\u4eab\u53d8\u91cf\u7ade\u4e89\u95ee\u9898\u3002while \u5faa\u73af\u4e2d\u7684 busy-wt \u4ee3\u7801\u5bfc\u81f4\u7ebf\u7a0b\u4e00\u76f4\u5360\u7528 cpu \u8d44\u6e90\uff0c\u65e0\u6cd5\u91ca\u653e\u9501\uff0c\u4ece\u800c\u9020\u6210\u6b7b\u9501\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u6b7b\u9501\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 wait() \u548c notify()\/notifyall() \u6765\u5b9e\u73b0\u7ebf\u7a0b\u4e4b\u95f4\u7684\u534f\u8c03\uff0c\u4ece\u800c\u907f\u514d\u5fd9\u7b49\u5f85\u3002<\/p>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class FooBar {\n    private int n;\n    private boolean flag = false;\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public synchronized void foo(Runnable printFoo) throws InterruptedException {\n        for (int i = 0; i &lt; n; i++) {\n            while (flag) {\n                wait();\n            }\n            \/\/ printFoo.run() outputs \"foo\". Do not change or remove this line.\n            printFoo.run();\n            flag = true;\n            notifyAll();\n        }\n    }\n\n    public synchronized void bar(Runnable printBar) throws InterruptedException {\n        for (int i = 0; i &lt; n; i++) {\n            while (!flag) {\n                wait();\n            }\n            \/\/ printBar.run() outputs \"bar\". Do not change or remove this line.\n            printBar.run();\n            flag = false;\n            notifyAll();\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4fee\u6539\u540e\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528 synchronized \u5173\u952e\u5b57\u4fdd\u8bc1\u4e86 foo \u548c bar \u65b9\u6cd5\u7684\u4e92\u65a5\u6267\u884c\u3002\u7ebf\u7a0b\u5728\u7b49\u5f85\u6761\u4ef6\u6ee1\u8db3\u65f6\u8c03\u7528 wait() \u65b9\u6cd5\u6765\u7b49\u5f85\uff0c\u5e76\u91ca\u653e\u9501\uff0c\u8ba9\u5176\u4ed6\u7ebf\u7a0b\u6709\u673a\u4f1a\u6267\u884c\u3002\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u4f1a\u8c03\u7528 notifyall() \u65b9\u6cd5\u6765\u5524\u9192\u5176\u4ed6\u7b49\u5f85\u7684\u7ebf\u7a0b\uff0c\u4ece\u800c\u907f\u514d\u6b7b\u9501\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 volatile \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370\u300cFooBar\u300d\u65f6\u5982\u4f55\u907f\u514d\u6b7b\u9501\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u4e24\u4e2a volatile \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u65f6\u6b7b\u9501\u95ee\u9898 \u9898\u76ee\u8981\u6c42\u8bbe\u8ba1\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4ea4\u66ff\u6253\u5370\u300cfoobar\u300d\u3002\u5f00\u53d1\u8005\u6309\u7167\u9898\u610f\u4f7f\u7528\u4e86\u4e24\u4e2a volatile \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u903b\u8f91\uff0c\u4f46\u8fd0\u884c\u540e\u7a0b\u5e8f\u5361\u6b7b\u5728 while \u5faa\u73af\u4e2d\u3002 \u539f\u56e0\u5206\u6790 \u4f7f\u7528 volatile \u53d8\u91cf\u786e\u5b9e\u53ef\u4ee5\u9632\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u4f46\u5b83\u4e0d\u80fd\u89e3\u51b3\u7ebf\u7a0b\u540c\u4e00\u7247\u6bb5\u5185\u5b58\u533a\u57df\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u5171\u4eab\u53d8\u91cf\u7ade\u4e89\u95ee\u9898\u3002while \u5faa\u73af\u4e2d\u7684 busy-wt \u4ee3\u7801\u5bfc\u81f4\u7ebf\u7a0b\u4e00\u76f4\u5360\u7528 cpu \u8d44\u6e90\uff0c\u65e0\u6cd5\u91ca\u653e\u9501\uff0c\u4ece\u800c\u9020\u6210\u6b7b\u9501\u3002 \u89e3\u51b3\u65b9\u6848 \u4e3a\u4e86\u89e3\u51b3\u6b7b\u9501\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 wait() \u548c notify()\/notifyall() \u6765\u5b9e\u73b0\u7ebf\u7a0b\u4e4b\u95f4\u7684\u534f\u8c03\uff0c\u4ece\u800c\u907f\u514d\u5fd9\u7b49\u5f85\u3002 \u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a class FooBar { private int n; private boolean flag = false; public FooBar(int n) { this.n = n; } public synchronized void foo(Runnable printFoo) throws InterruptedException { for (int i = [&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-64998","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64998","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=64998"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64998\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}