{"id":36356,"date":"2024-11-26T13:30:30","date_gmt":"2024-11-26T05:30:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36356\/"},"modified":"2024-11-26T13:30:30","modified_gmt":"2024-11-26T05:30:30","slug":"%e4%bd%bf%e7%94%a8-volatile-%e8%a7%a3%e5%86%b3%e4%ba%a4%e6%9b%bf%e6%89%93%e5%8d%b0-foobar-%e9%81%87%e5%88%b0%e5%8d%a1%e6%ad%bb%e9%97%ae%e9%a2%98%e7%9a%84%e6%a0%b9%e6%9c%ac%e5%8e%9f%e5%9b%a0%e6%98%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36356\/","title":{"rendered":"\u4f7f\u7528 volatile \u89e3\u51b3\u4ea4\u66ff\u6253\u5370 FooBar \u9047\u5230\u5361\u6b7b\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111418483220.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 volatile \u89e3\u51b3\u4ea4\u66ff\u6253\u5370 FooBar \u9047\u5230\u5361\u6b7b\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 volatile \u89e3\u51b3\u4ea4\u66ff\u6253\u5370 FooBar \u9047\u5230\u5361\u6b7b\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 foobar \u9047\u5230\u5361\u6b7b\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 1115 \u9898\u300c\u4ea4\u66ff\u6253\u5370 foobar\u300d\u65f6\uff0c\u6253\u7b97\u4f7f\u7528 2 \u4e2a volatile boolean \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u903b\u8f91\uff0c\u5374\u9047\u5230\u4e86\u5361\u6b7b\u5728 while \u5faa\u73af\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u95ee\u9898\u5206\u6790<\/strong><\/p>\n<p>\u4f7f\u7528 volatile \u53d8\u91cf\u4e0d\u4f1a\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u4f46\u4ecd\u7136\u5361\u6b7b\u7684\u539f\u56e0\u5728\u4e8e while \u5faa\u73af\u9020\u6210\u7684\u300c\u5fd9\u7b49\u5f85\u300d\u3002\u7ebf\u7a0b\u6301\u7eed\u5360\u7528 cpu \u8d44\u6e90\uff0c\u5bfc\u81f4\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u65f6\u7684\u5524\u9192\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 wt() \u548c notify()<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 wait() \u548c notify() \u65b9\u6cd5\u6765\u5b9e\u73b0\u7ebf\u7a0b\u4e4b\u95f4\u7684\u534f\u8c03\u3002\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u8c03\u7528 wait() \u65b9\u6cd5\u7b49\u5f85\uff0c\u5e76\u91ca\u653e\u9501\uff0c\u5141\u8bb8\u5176\u4ed6\u7ebf\u7a0b\u6267\u884c\u3002\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u8c03\u7528 notifyall() \u65b9\u6cd5\u5524\u9192\u6240\u6709\u7b49\u5f85\u7684\u7ebf\u7a0b\u3002<\/p>\n<p><strong>\u4ee3\u7801\u4fee\u6539\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 wait() \u548c notify() \u4fee\u6539\u540e\u7684\u4ee3\u7801\u793a\u4f8b\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\u786e\u4fdd foo \u548c bar \u65b9\u6cd5\u7684\u4e92\u65a5\u6267\u884c\u3002\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u8c03\u7528 wait() \u65b9\u6cd5\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\u8c03\u7528 notifyall() \u65b9\u6cd5\u5524\u9192\u6240\u6709\u7b49\u5f85\u7684\u7ebf\u7a0b\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 volatile \u89e3\u51b3\u4ea4\u66ff\u6253\u5370 FooBar \u9047\u5230\u5361\u6b7b\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u5370 foobar \u9047\u5230\u5361\u6b7b\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5 \u95ee\u9898\u63cf\u8ff0 \u5728\u4f7f\u7528 1115 \u9898\u300c\u4ea4\u66ff\u6253\u5370 foobar\u300d\u65f6\uff0c\u6253\u7b97\u4f7f\u7528 2 \u4e2a volatile boolean \u53d8\u91cf\u63a7\u5236\u591a\u7ebf\u7a0b\u903b\u8f91\uff0c\u5374\u9047\u5230\u4e86\u5361\u6b7b\u5728 while \u5faa\u73af\u4e2d\u7684\u95ee\u9898\u3002 \u95ee\u9898\u5206\u6790 \u4f7f\u7528 volatile \u53d8\u91cf\u4e0d\u4f1a\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u4f46\u4ecd\u7136\u5361\u6b7b\u7684\u539f\u56e0\u5728\u4e8e while \u5faa\u73af\u9020\u6210\u7684\u300c\u5fd9\u7b49\u5f85\u300d\u3002\u7ebf\u7a0b\u6301\u7eed\u5360\u7528 cpu \u8d44\u6e90\uff0c\u5bfc\u81f4\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u65f6\u7684\u5524\u9192\u3002 \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 wt() \u548c notify() \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 wait() \u548c notify() \u65b9\u6cd5\u6765\u5b9e\u73b0\u7ebf\u7a0b\u4e4b\u95f4\u7684\u534f\u8c03\u3002\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u8c03\u7528 wait() \u65b9\u6cd5\u7b49\u5f85\uff0c\u5e76\u91ca\u653e\u9501\uff0c\u5141\u8bb8\u5176\u4ed6\u7ebf\u7a0b\u6267\u884c\u3002\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u8c03\u7528 notifyall() \u65b9\u6cd5\u5524\u9192\u6240\u6709\u7b49\u5f85\u7684\u7ebf\u7a0b\u3002 \u4ee3\u7801\u4fee\u6539\u793a\u4f8b \u4ee5\u4e0b\u662f\u4f7f\u7528 wait() \u548c notify() \u4fee\u6539\u540e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a class FooBar { private int n; private boolean flag = false; public FooBar(int [&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-36356","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36356","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=36356"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36356\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}