{"id":71136,"date":"2023-12-30T22:15:17","date_gmt":"2023-12-30T14:15:17","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=71136"},"modified":"2023-12-30T22:15:17","modified_gmt":"2023-12-30T14:15:17","slug":"java-memory-jmm-analysis","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-memory-jmm-analysis.html","title":{"rendered":"\u89e3\u6790Java \u5185\u5b58\u6a21\u578b\u2014\u2014 JMM"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u4e3b\u8981\u4ecb\u7ecdJMM\u539f\u7406\u3002Java Memory Model\u7b80\u79f0JMM, \u662f\u4e00\u7cfb\u5217\u7684Java\u865a\u62df\u673a\u5e73\u53f0\u5bf9\u5f00\u53d1\u8005\u63d0\u4f9b\u7684\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u5185\u5b58\u53ef\u89c1\u6027\u3001\u662f\u5426\u53ef\u4ee5\u91cd\u6392\u5e8f\u7b49\u95ee\u9898\u7684\u65e0\u5173\u5177\u4f53\u5e73\u53f0\u7684\u7edf\u4e00\u7684\u4fdd\u8bc1\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
JMM\u7b80\u4ecb<\/strong><\/div>\n

Java Memory Model\u7b80\u79f0JMM, \u662f\u4e00\u7cfb\u5217\u7684Java\u865a\u62df\u673a\u5e73\u53f0\u5bf9\u5f00\u53d1\u8005\u63d0\u4f9b\u7684\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u5185\u5b58\u53ef\u89c1\u6027\u3001\u662f\u5426\u53ef\u4ee5\u91cd\u6392\u5e8f\u7b49\u95ee\u9898\u7684\u65e0\u5173\u5177\u4f53\u5e73\u53f0\u7684\u7edf\u4e00\u7684\u4fdd\u8bc1\u3002(\u53ef\u80fd\u5728\u672f\u8bed\u4e0a\u4e0eJava\u8fd0\u884c\u65f6\u5185\u5b58\u5206\u5e03\u6709\u6b67\u4e49\uff0c\u540e\u8005\u6307\u5806\u3001\u65b9\u6cd5\u533a\u3001\u7ebf\u7a0b\u6808\u7b49\u5185\u5b58\u533a\u57df)\u3002
\n\u5e76\u53d1\u7f16\u7a0b\u6709\u591a\u79cd\u98ce\u683c\uff0c\u9664\u4e86CSP(\u901a\u4fe1\u987a\u5e8f\u8fdb\u7a0b)\u3001Actor\u7b49\u6a21\u578b\u5916\uff0c\u5927\u5bb6\u6700\u719f\u6089\u7684\u5e94\u8be5\u662f\u57fa\u4e8e\u7ebf\u7a0b\u548c\u9501\u7684\u5171\u4eab\u5185\u5b58\u6a21\u578b\u4e86\u3002\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4e09\u7c7b\u5e76\u53d1\u95ee\u9898:<\/p>\n

    \n
  1. \u539f\u5b50\u6027<\/li>\n
  2. \u53ef\u89c1\u6027<\/li>\n
  3. \u91cd\u6392\u5e8f<\/li>\n<\/ol>\n

    \u539f\u5b50\u6027\u6d89\u53ca\u5230\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u4e00\u4e2a\u590d\u5408\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u5176\u4ed6\u7ebf\u7a0b\u662f\u5426\u80fd\u591f\u770b\u5230\u4e2d\u95f4\u7684\u72b6\u6001\u3001\u6216\u8fdb\u884c\u5e72\u6270\u3002\u5178\u578b\u7684\u5c31\u662fi++\u7684\u95ee\u9898\u4e86\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u5171\u4eab\u7684\u5806\u5185\u5b58\u6267\u884c++\u64cd\u4f5c\uff0c\u800c++\u64cd\u4f5c\u5728JVM\u3001\u8fd0\u884c\u65f6\u3001CPU\u4e2d\u7684\u5b9e\u73b0\u90fd\u53ef\u80fd\u662f\u4e00\u4e2a\u590d\u5408\u64cd\u4f5c, \u4f8b\u5982\u5728JVM\u6307\u4ee4\u7684\u89d2\u5ea6\u6765\u770b\u662f\u5c06i\u7684\u503c\u4ece\u5806\u5185\u5b58\u8bfb\u5230\u64cd\u4f5c\u6570\u6808\u3001\u52a0\u4e0a\u4e00\u3001\u518d\u5199\u56de\u5230\u5806\u5185\u5b58\u7684i\uff0c\u8fd9\u51e0\u4e2a\u64cd\u4f5c\u7684\u671f\u95f4\uff0c\u5982\u679c\u6ca1\u6709\u6b63\u786e\u7684\u540c\u6b65\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4e5f\u53ef\u4ee5\u540c\u65f6\u6267\u884c\uff0c\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u7b49\u95ee\u9898\u3002\u5e38\u89c1\u7684\u539f\u5b50\u6027\u95ee\u9898\u53c8\u53eb\u7ade\u592a\u6761\u4ef6\uff0c\u662f\u57fa\u4e8e\u4e00\u4e2a\u53ef\u80fd\u5931\u6548\u7684\u7ed3\u679c\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u8bfb\u53d6-\u4fee\u6539-\u5199\u5165\u3002\u00a0\u53ef\u89c1\u6027\u548c\u91cd\u6392\u5e8f\u95ee\u9898\u90fd\u6e90\u4e8e\u7cfb\u7edf\u7684\u4f18\u5316\u3002<\/p>\n

    \u7531\u4e8eCPU\u7684\u6267\u884c\u901f\u5ea6\u548c\u5185\u5b58\u7684\u5b58\u53d6\u901f\u5ea6\u4e25\u91cd\u4e0d\u5339\u914d\uff0c\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u57fa\u4e8e\u65f6\u95f4\u5c40\u90e8\u6027\u3001\u7a7a\u95f4\u5c40\u90e8\u6027\u7b49\u5c40\u90e8\u6027\u539f\u7406\uff0cCPU\u5728\u548c\u5185\u5b58\u95f4\u589e\u52a0\u4e86\u591a\u5c42\u9ad8\u901f\u7f13\u5b58\uff0c\u5f53\u9700\u8981\u53d6\u6570\u636e\u65f6\uff0cCPU\u4f1a\u5148\u5230\u9ad8\u901f\u7f13\u5b58\u4e2d\u67e5\u627e\u5bf9\u5e94\u7684\u7f13\u5b58\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u5230\u5185\u5b58\u4e2d\u53d6\u51fa\u5e76\u4fdd\u5b58\u5728\u9ad8\u901f\u7f13\u5b58\u4e2d\u3002\u73b0\u5728\u591a\u6838\u5904\u7406\u5668\u8d8a\u57fa\u672c\u5df2\u7ecf\u6210\u4e3a\u6807\u914d\uff0c\u8fd9\u65f6\u6bcf\u4e2a\u5904\u7406\u5668\u90fd\u6709\u81ea\u5df1\u7684\u7f13\u5b58\uff0c\u8fd9\u5c31\u6d89\u53ca\u5230\u4e86\u7f13\u5b58\u4e00\u81f4\u6027\u7684\u95ee\u9898\uff0cCPU\u6709\u4e0d\u540c\u5f3a\u5f31\u7684\u4e00\u81f4\u6027\u6a21\u578b\uff0c\u6700\u5f3a\u7684\u4e00\u81f4\u6027\u5b89\u5168\u6027\u6700\u9ad8\uff0c\u4e5f\u7b26\u5408\u6211\u4eec\u7684\u987a\u5e8f\u601d\u8003\u7684\u6a21\u5f0f\uff0c\u4f46\u662f\u5728\u6027\u80fd\u4e0a\u56e0\u4e3a\u9700\u8981\u4e0d\u540cCPU\u4e4b\u95f4\u7684\u534f\u8c03\u901a\u4fe1\u5c31\u4f1a\u6709\u5f88\u591a\u5f00\u9500\u3002<\/p>\n

    \u5178\u578b\u7684CPU\u7f13\u5b58\u7ed3\u6784\u793a\u610f\u56fe\u5982\u4e0b<\/p>\n

    \"Java<\/p>\n

    CPU\u7684\u6307\u4ee4\u5468\u671f\u901a\u5e38\u4e3a\u53d6\u6307\u4ee4\u3001\u89e3\u6790\u6307\u4ee4\u8bfb\u53d6\u6570\u636e\u3001\u6267\u884c\u6307\u4ee4\u3001\u6570\u636e\u5199\u56de\u5bc4\u5b58\u5668\u6216\u5185\u5b58\u3002\u4e32\u884c\u6267\u884c\u6307\u4ee4\u65f6\u5176\u4e2d\u7684\u8bfb\u53d6\u5b58\u50a8\u6570\u636e\u90e8\u5206\u5360\u7528\u65f6\u95f4\u8f83\u957f\uff0c\u6240\u4ee5CPU\u666e\u904d\u91c7\u53d6\u6307\u4ee4\u6d41\u6c34\u7ebf\u7684\u65b9\u5f0f\u540c\u65f6\u6267\u884c\u591a\u4e2a\u6307\u4ee4, \u63d0\u9ad8\u6574\u4f53\u541e\u5410\u7387\uff0c\u5c31\u50cf\u5de5\u5382\u6d41\u6c34\u7ebf\u4e00\u6837\u3002<\/p>\n

    \"Java<\/p>\n

    \u8bfb\u53d6\u6570\u636e\u548c\u5199\u56de\u6570\u636e\u5230\u5185\u5b58\u76f8\u6bd4\u6267\u884c\u6307\u4ee4\u7684\u901f\u5ea6\u4e0d\u5728\u4e00\u4e2a\u6570\u91cf\u7ea7\u4e0a\uff0c\u6240\u4ee5CPU\u4f7f\u7528\u5bc4\u5b58\u5668\u3001\u9ad8\u901f\u7f13\u5b58\u4f5c\u4e3a\u7f13\u5b58\u548c\u7f13\u51b2\uff0c\u5728\u4ece\u5185\u5b58\u4e2d\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u4f1a\u8bfb\u53d6\u4e00\u4e2a\u7f13\u5b58\u884c(cache line)\u7684\u6570\u636e\uff08\u7c7b\u4f3c\u78c1\u76d8\u8bfb\u53d6\u8bfb\u53d6\u4e00\u4e2ablock\uff09\u3002\u6570\u636e\u5199\u56de\u7684\u6a21\u5757\u5728\u65e7\u6570\u636e\u6ca1\u6709\u5728\u7f13\u5b58\u4e2d\u7684\u60c5\u51b5\u4e0b\u4f1a\u5c06\u5b58\u50a8\u8bf7\u6c42\u653e\u5165\u4e00\u4e2astore buffer\u4e2d\u7ee7\u7eed\u6267\u884c\u6307\u4ee4\u5468\u671f\u7684\u4e0b\u4e00\u4e2a\u9636\u6bb5\uff0c\u5982\u679c\u5b58\u5728\u4e8e\u7f13\u5b58\u4e2d\u5219\u4f1a\u66f4\u65b0\u7f13\u5b58\uff0c\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u4f1a\u6839\u636e\u4e00\u5b9a\u7b56\u7565flush\u5230\u5185\u5b58\u3002<\/p>\n

    public class MemoryModel {\r\n    private int count;\r\n    private boolean stop;\r\n    public void initCountAndStop() {\r\n        count = 1;\r\n        stop = false;\r\n    }\r\n    public void doLoop() {\r\n        while(!stop) {\r\n            count++;\r\n        }\r\n    }\r\n    public void printResult() {\r\n        System.out.println(count);\r\n        System.out.println(stop);\r\n    }\r\n}<\/pre>\n

    \u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u6267\u884c\u65f6\u6211\u4eec\u53ef\u80fd\u8ba4\u4e3acount = 1\u4f1a\u5728stop = false\u524d\u6267\u884c\u5b8c\u6210\uff0c\u8fd9\u5728\u4e0a\u9762\u7684CPU\u6267\u884c\u56fe\u4e2d\u663e\u793a\u7684\u7406\u60f3\u72b6\u6001\u4e0b\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f\u8981\u8003\u8651\u4e0a\u5bc4\u5b58\u5668\u3001\u7f13\u5b58\u7f13\u51b2\u7684\u65f6\u5019\u5c31\u4e0d\u6b63\u786e\u4e86, \u4f8b\u5982stop\u672c\u8eab\u5728\u7f13\u5b58\u4e2d\u4f46\u662fcount\u4e0d\u5728\uff0c\u5219\u53ef\u80fdstop\u66f4\u65b0\u540e\u518dcount\u7684write buffer\u5199\u56de\u4e4b\u524d\u5237\u65b0\u5230\u4e86\u5185\u5b58\u3002<\/p>\n

    \u53e6\u5916CPU\u3001\u7f16\u8bd1\u5668\uff08\u5bf9\u4e8eJava\u4e00\u822c\u6307JIT\uff09\u90fd\u53ef\u80fd\u4f1a\u4fee\u6539\u6307\u4ee4\u6267\u884c\u987a\u5e8f\uff0c\u4f8b\u5982\u4e0a\u8ff0\u4ee3\u7801\u4e2dcount = 1\u548cstop = false\u4e24\u8005\u5e76\u6ca1\u6709\u4f9d\u8d56\u5173\u7cfb\uff0c\u6240\u4ee5CPU\u3001\u7f16\u8bd1\u5668\u90fd\u6709\u53ef\u80fd\u4fee\u6539\u8fd9\u4e24\u8005\u7684\u987a\u5e8f\uff0c\u800c\u5728\u5355\u7ebf\u7a0b\u6267\u884c\u7684\u7a0b\u5e8f\u770b\u6765\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u4e5f\u662fCPU\u3001\u7f16\u8bd1\u5668\u8981\u4fdd\u8bc1\u7684as-if-serial(\u4e0d\u7ba1\u5982\u4f55\u4fee\u6539\u6267\u884c\u987a\u5e8f\uff0c\u5355\u7ebf\u7a0b\u7684\u6267\u884c\u7ed3\u679c\u4e0d\u53d8)\u3002\u7531\u4e8e\u5f88\u5927\u90e8\u5206\u7a0b\u5e8f\u6267\u884c\u90fd\u662f\u5355\u7ebf\u7a0b\u7684\uff0c\u6240\u4ee5\u8fd9\u6837\u7684\u4f18\u5316\u662f\u53ef\u4ee5\u63a5\u53d7\u5e76\u4e14\u5e26\u6765\u4e86\u8f83\u5927\u7684\u6027\u80fd\u63d0\u5347\u3002\u4f46\u662f\u5728\u591a\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u8fdb\u884c\u5fc5\u8981\u7684\u540c\u6b65\u64cd\u4f5c\u5219\u53ef\u80fd\u4f1a\u51fa\u73b0\u4ee4\u4eba\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u4f8b\u5982\u5728\u7ebf\u7a0bT1\u6267\u884c\u5b8cinitCountAndStop\u65b9\u6cd5\u540e\uff0c\u7ebf\u7a0bT2\u6267\u884cprintResult\uff0c\u5f97\u5230\u7684\u53ef\u80fd\u662f0, false, \u53ef\u80fd\u662f1, false, \u4e5f\u53ef\u80fd\u662f0, true\u3002\u5982\u679c\u7ebf\u7a0bT1\u5148\u6267\u884cdoLoop()\uff0c\u7ebf\u7a0bT2\u4e00\u79d2\u540e\u6267\u884cinitCountAndStop, \u5219T1\u53ef\u80fd\u4f1a\u8df3\u51fa\u5faa\u73af\u3001\u4e5f\u53ef\u80fd\u7531\u4e8e\u7f16\u8bd1\u5668\u7684\u4f18\u5316\u6c38\u8fdc\u65e0\u6cd5\u770b\u5230stop\u7684\u4fee\u6539\u3002<\/p>\n

    \u7531\u4e8e\u4e0a\u8ff0\u8fd9\u4e9b\u591a\u7ebf\u7a0b\u60c5\u51b5\u4e0b\u7684\u5404\u79cd\u95ee\u9898\uff0c\u591a\u7ebf\u7a0b\u4e2d\u7684\u7a0b\u5e8f\u987a\u5e8f\u5df2\u7ecf\u4e0d\u662f\u5e95\u5c42\u673a\u5236\u4e2d\u7684\u6267\u884c\u987a\u5e8f\u548c\u7ed3\u679c\uff0c\u7f16\u7a0b\u8bed\u8a00\u9700\u8981\u7ed9\u5f00\u53d1\u8005\u4e00\u79cd\u4fdd\u8bc1\uff0c\u8fd9\u4e2a\u4fdd\u8bc1\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u4e00\u4e2a\u7ebf\u7a0b\u7684\u4fee\u6539\u4f55\u65f6\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u53ef\u89c1\uff0c\u56e0\u6b64Java\u8bed\u8a00\u63d0\u51fa\u4e86JavaMemoryModel\u5373Java\u5185\u5b58\u6a21\u578b\uff0c\u5bf9\u4e8eJava\u8bed\u8a00\u3001JVM\u3001\u7f16\u8bd1\u5668\u7b49\u5b9e\u73b0\u8005\u9700\u8981\u6309\u7167\u8fd9\u4e2a\u6a21\u578b\u7684\u7ea6\u5b9a\u6765\u8fdb\u884c\u5b9e\u73b0\u3002Java\u63d0\u4f9b\u4e86Volatile<\/a>\u3001synchronized\u3001final\u7b49\u673a\u5236\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5728\u6240\u6709\u5904\u7406\u5668\u5e73\u53f0\u4e0a\u7684\u6b63\u786e\u6027\u3002<\/p>\n

    \u5728JDK1.5\u4e4b\u524d\uff0cJava\u7684\u5185\u5b58\u6a21\u578b\u6709\u7740\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u4f8b\u5982\u5728\u65e7\u7684\u5185\u5b58\u6a21\u578b\u4e2d\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u5728\u6784\u9020\u5668\u6267\u884c\u5b8c\u6210\u540e\u770b\u5230\u4e00\u4e2afinal\u5b57\u6bb5\u7684\u9ed8\u8ba4\u503c\u3001volatile\u5b57\u6bb5\u7684\u5199\u5165\u53ef\u80fd\u4f1a\u548c\u975evolatile\u5b57\u6bb5\u7684\u8bfb\u5199\u91cd\u6392\u5e8f\u3002<\/p>\n

    \u6240\u4ee5\u5728JDK1.5\u4e2d\uff0c\u901a\u8fc7JSR133\u63d0\u51fa\u4e86\u65b0\u7684\u5185\u5b58\u6a21\u578b\uff0c\u4fee\u590d\u4e4b\u524d\u51fa\u73b0\u7684\u95ee\u9898\u3002<\/p>\n

    \u91cd\u6392\u5e8f\u89c4\u5219<\/strong><\/div>\n
    volatile\u548c\u76d1\u89c6\u5668\u9501<\/strong><\/span><\/div>\n\n\n\n\n\n\n\n\n
    \u662f\u5426\u53ef\u4ee5\u91cd\u6392\u5e8f<\/th>\n\u7b2c\u4e8c\u4e2a\u64cd\u4f5c<\/th>\n\u7b2c\u4e8c\u4e2a\u64cd\u4f5c<\/th>\n\u7b2c\u4e8c\u4e2a\u64cd\u4f5c<\/th>\n<\/tr>\n<\/thead>\n
    \u7b2c\u4e00\u4e2a\u64cd\u4f5c<\/td>\n\u666e\u901a\u8bfb\/\u666e\u901a\u5199<\/td>\nvolatile\u8bfb\/monitor enter<\/td>\nvolatile\u5199\/monitor exit<\/td>\n<\/tr>\n
    \u666e\u901a\u8bfb\/\u666e\u901a\u5199<\/td>\n<\/td>\n<\/td>\nNo<\/td>\n<\/tr>\n
    voaltile\u8bfb\/monitor enter<\/td>\nNo<\/td>\nNo<\/td>\nNo<\/td>\n<\/tr>\n
    volatile\u5199\/monitor exit<\/td>\n<\/td>\nNo<\/td>\nNo<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

    \u5176\u4e2d\u666e\u901a\u8bfb\u6307getfield, getstatic, \u975evolatile\u6570\u7ec4\u7684arrayload, \u666e\u901a\u5199\u6307putfield, putstatic, \u975evolatile\u6570\u7ec4\u7684arraystore\u3002<\/p>\n

    volatile\u8bfb\u5199\u5206\u522b\u662fvolatile\u5b57\u6bb5\u7684getfield, getstatic\u548cputfield, putstatic\u3002<\/p>\n

    monitorenter\u662f\u8fdb\u5165\u540c\u6b65\u5757\u6216\u540c\u6b65\u65b9\u6cd5,monitorexist\u6307\u9000\u51fa\u540c\u6b65\u5757\u6216\u540c\u6b65\u65b9\u6cd5\u3002<\/p>\n

    \u4e0a\u8ff0\u8868\u683c\u4e2d\u7684No\u6307\u5148\u540e\u4e24\u4e2a\u64cd\u4f5c\u4e0d\u5141\u8bb8\u91cd\u6392\u5e8f\uff0c\u5982(\u666e\u901a\u5199, volatile\u5199)\u6307\u975evolatile\u5b57\u6bb5\u7684\u5199\u5165\u4e0d\u80fd\u548c\u4e4b\u540e\u4efb\u610f\u7684volatile\u5b57\u6bb5\u7684\u5199\u5165\u91cd\u6392\u5e8f\u3002\u5f53\u6ca1\u6709No\u65f6\uff0c\u8bf4\u660e\u91cd\u6392\u5e8f\u662f\u5141\u8bb8\u7684\uff0c\u4f46\u662fJVM\u9700\u8981\u4fdd\u8bc1\u6700\u5c0f\u5b89\u5168\u6027-\u8bfb\u53d6\u7684\u503c\u8981\u4e48\u662f\u9ed8\u8ba4\u503c\uff0c\u8981\u4e48\u662f\u5176\u4ed6\u7ebf\u7a0b\u5199\u5165\u7684\uff0864\u4f4d\u7684double\u548clong\u8bfb\u5199\u64cd\u4f5c\u662f\u4e2a\u7279\u4f8b\uff0c\u5f53\u6ca1\u6709volatile\u4fee\u9970\u65f6\uff0c\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u8bfb\u5199\u662f\u539f\u5b50\u7684\uff0c\u5e95\u5c42\u53ef\u80fd\u5c06\u5176\u62c6\u5206\u4e3a\u4e24\u4e2a\u5355\u72ec\u7684\u64cd\u4f5c\uff09\u3002<\/p>\n

    final\u5b57\u6bb5<\/strong><\/span><\/div>\n

    final\u5b57\u6bb5\u6709\u4e24\u4e2a\u989d\u5916\u7684\u7279\u6b8a\u89c4\u5219<\/p>\n

      \n
    1. final\u5b57\u6bb5\u7684\u5199\u5165\uff08\u5728\u6784\u9020\u5668\u4e2d\u8fdb\u884c\uff09\u4ee5\u53cafinal\u5b57\u6bb5\u5bf9\u8c61\u672c\u8eab\u7684\u5f15\u7528\u7684\u5199\u5165\u90fd\u4e0d\u80fd\u548c\u540e\u7eed\u7684\uff08\u6784\u9020\u5668\u5916\u7684\uff09\u6301\u6709\u8be5final\u5b57\u6bb5\u7684\u5bf9\u8c61\u7684\u5199\u5165\u91cd\u6392\u5e8f\u3002\u4f8b\u5982, \u4e0b\u9762\u7684\u8bed\u53e5\u662f\u4e0d\u80fd\u91cd\u6392\u5e8f\u7684\n
      x.finalField = v; ...; sharedRef = x;<\/pre>\n<\/li>\n
    2. final\u5b57\u6bb5\u7684\u7b2c\u4e00\u6b21\u52a0\u8f7d\u4e0d\u80fd\u548c\u6301\u6709\u8fd9\u4e2afinal\u5b57\u6bb5\u7684\u5bf9\u8c61\u7684\u5199\u5165\u91cd\u6392\u5e8f\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u8bed\u53e5\u662f\u4e0d\u5141\u8bb8\u91cd\u6392\u5e8f\u7684\n
      x = sharedRef; ...; i = x.finalField<\/pre>\n<\/li>\n<\/ol>\n
      \u5185\u5b58\u5c4f\u969c<\/strong><\/span><\/div>\n

      \u5904\u7406\u5668\u90fd\u652f\u6301\u4e00\u5b9a\u7684\u5185\u5b58\u5c4f\u969c(memory barrier)\u6216\u6805\u680f(fence)\u6765\u63a7\u5236\u91cd\u6392\u5e8f\u548c\u6570\u636e\u5728\u4e0d\u540c\u7684\u5904\u7406\u5668\u95f4\u7684\u53ef\u89c1\u6027\u3002\u4f8b\u5982\uff0cCPU\u5c06\u6570\u636e\u5199\u56de\u65f6\uff0c\u4f1a\u5c06store\u8bf7\u6c42\u653e\u5165write buffer\u4e2d\u7b49\u5f85flush\u5230\u5185\u5b58\uff0c\u53ef\u4ee5\u901a\u8fc7\u63d2\u5165barrier\u7684\u65b9\u5f0f\u9632\u6b62\u8fd9\u4e2astore\u8bf7\u6c42\u4e0e\u5176\u4ed6\u7684\u8bf7\u6c42\u91cd\u6392\u5e8f\u3001\u4fdd\u8bc1\u6570\u636e\u7684\u53ef\u89c1\u6027\u3002\u53ef\u4ee5\u7528\u4e00\u4e2a\u751f\u6d3b\u4e2d\u7684\u4f8b\u5b50\u7c7b\u6bd4\u5c4f\u969c\uff0c\u4f8b\u5982\u5750\u5730\u94c1\u7684\u659c\u5761\u5f0f\u7535\u68af\u65f6\uff0c\u5927\u5bb6\u6309\u987a\u5e8f\u8fdb\u5165\u7535\u68af\uff0c\u4f46\u662f\u4f1a\u6709\u4e00\u4e9b\u4eba\u4ece\u5de6\u4fa7\u7ed5\u8fc7\u53bb\uff0c\u8fd9\u6837\u51fa\u7535\u68af\u65f6\u987a\u5e8f\u5c31\u4e0d\u76f8\u540c\u4e86\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u4eba\u643a\u5e26\u4e86\u4e00\u4e2a\u5927\u7684\u884c\u674e\u5835\u4f4f\u4e86\uff08\u5c4f\u969c\uff09\uff0c\u5219\u540e\u9762\u7684\u4eba\u5c31\u4e0d\u80fd\u7ed5\u8fc7\u53bb\u4e86:)\u3002\u53e6\u5916\u8fd9\u91cc\u7684barrier\u548cGC\u4e2d\u7528\u5230\u7684write barrier\u662f\u4e0d\u540c\u7684\u6982\u5ff5\u3002<\/p>\n

      \u5185\u5b58\u5c4f\u969c\u7684\u5206\u7c7b<\/strong><\/p>\n

      \u51e0\u4e4e\u6240\u6709\u7684\u5904\u7406\u5668\u90fd\u652f\u6301\u4e00\u5b9a\u7c97\u7c92\u5ea6\u7684barrier\u6307\u4ee4\uff0c\u901a\u5e38\u53eb\u505aFence(\u6805\u680f\u3001\u56f4\u5899)\uff0c\u80fd\u591f\u4fdd\u8bc1\u5728fence\u4e4b\u524d\u53d1\u8d77\u7684load\u548cstore\u6307\u4ee4\u90fd\u80fd\u4e25\u683c\u7684\u548cfence\u4e4b\u540e\u7684load\u548cstore\u4fdd\u6301\u6709\u5e8f\u3002\u901a\u5e38\u6309\u7167\u7528\u9014\u4f1a\u5206\u4e3a\u4e0b\u9762\u56db\u79cdbarrier<\/p>\n

      LoadLoad Barriers<\/strong><\/p>\n

      Load1; LoadLoad; Load2;<\/p>\n

      \u4fdd\u8bc1Load1\u7684\u6570\u636e\u5728Load2\u53ca\u4e4b\u540e\u7684load\u524d\u52a0\u8f7d<\/p>\n

      StoreStore Barriers<\/strong><\/p>\n

      Store1; StoreStore; Store2<\/p>\n

      \u4fdd\u8bc1Store1\u7684\u6570\u636e\u5148\u4e8eStore2\u53ca\u4e4b\u540e\u7684\u6570\u636e \u5728\u5176\u4ed6\u5904\u7406\u5668\u53ef\u89c1<\/p>\n

      LoadStore Barriers<\/strong><\/p>\n

      Load1; LoadStore; Store2<\/p>\n

      \u4fdd\u8bc1Load1\u7684\u6570\u636e\u7684\u52a0\u8f7d\u5728Store2\u548c\u4e4b\u540e\u7684\u6570\u636eflush\u524d<\/p>\n

      StoreLoad Barriers<\/strong><\/p>\n

      Store1; StoreLoad; Load2<\/p>\n

      \u4fdd\u8bc1Store1\u7684\u6570\u636e\u5728\u5176\u4ed6\u5904\u7406\u5668\u524d\u53ef\u89c1(\u5982flush\u5230\u5185\u5b58)\u5148\u4e8eLoad2\u548c\u4e4b\u540e\u7684load\u7684\u6570\u636e\u7684\u52a0\u8f7d\u3002StoreLoad Barrier\u80fd\u591f\u9632\u6b62load\u8bfb\u53d6\u5230\u65e7\u6570\u636e\u800c\u4e0d\u662f\u6700\u8fd1\u5176\u4ed6\u5904\u7406\u5668\u5199\u5165\u7684\u6570\u636e\u3002<\/p>\n

      \u51e0\u4e4e\u8fd1\u4ee3\u7684\u6240\u6709\u7684\u591a\u5904\u7406\u5668\u90fd\u9700\u8981StoreLoad\uff0cStoreLoad\u7684\u5f00\u9500\u901a\u5e38\u662f\u6700\u5927\u7684\uff0c\u5e76\u4e14StoreLoad\u5177\u6709\u5176\u4ed6\u4e09\u79cd\u5c4f\u969c\u7684\u6548\u679c\uff0c\u6240\u4ee5StoreLoad\u53ef\u4ee5\u5f53\u505a\u4e00\u4e2a\u901a\u7528\u7684(\u4f46\u662f\u66f4\u9ad8\u5f00\u9500\u7684)\u5c4f\u969c\u3002<\/p>\n

      \u6240\u4ee5\uff0c\u5229\u7528\u4e0a\u8ff0\u7684\u5185\u5b58\u5c4f\u969c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u9762\u8868\u683c\u4e2d\u7684\u91cd\u6392\u5e8f\u89c4\u5219<\/p>\n\n\n\n\n\n\n\n\n\n
      \u9700\u8981\u7684\u5c4f\u969c<\/th>\n\u7b2c\u4e8c\u4e2a\u64cd\u4f5c<\/th>\n\u7b2c\u4e8c\u4e2a\u64cd\u4f5c<\/th>\n\u7b2c\u4e8c\u4e2a\u64cd\u4f5c<\/th>\n\u7b2c\u4e8c\u4e2a\u64cd\u4f5c<\/th>\n<\/tr>\n<\/thead>\n
      \u7b2c\u4e00\u4e2a\u64cd\u4f5c<\/td>\n\u666e\u901a\u8bfb<\/td>\n\u666e\u901a\u5199<\/td>\nvolatile\u8bfb\/monitor enter<\/td>\nvolatile\u5199\/monitor exit<\/td>\n<\/tr>\n
      \u666e\u901a\u8bfb<\/td>\n<\/td>\n<\/td>\n<\/td>\nLoadStore<\/td>\n<\/tr>\n
      \u666e\u901a\u8bfb<\/td>\n<\/td>\n<\/td>\n<\/td>\nStoreStore<\/td>\n<\/tr>\n
      voaltile\u8bfb\/monitor enter<\/td>\nLoadLoad<\/td>\nLoadStore<\/td>\nLoadLoad<\/td>\nLoadStore<\/td>\n<\/tr>\n
      volatile\u5199\/monitor exit<\/td>\n<\/td>\n<\/td>\nStoreLoad<\/td>\nStoreStore<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

      \u4e3a\u4e86\u652f\u6301final\u5b57\u6bb5\u7684\u89c4\u5219\uff0c\u9700\u8981\u5bf9final\u7684\u5199\u5165\u589e\u52a0barrier<\/p>\n

      x.finalField = v; StoreStore; sharedRef = x;<\/p>\n

      \u63d2\u5165\u5185\u5b58\u5c4f\u969c<\/strong><\/p>\n

      \u57fa\u4e8e\u4e0a\u9762\u7684\u89c4\u5219\uff0c\u53ef\u4ee5\u5728volatile\u5b57\u6bb5\u3001synchronized\u5173\u952e\u5b57\u7684\u5904\u7406\u4e0a\u589e\u52a0\u5c4f\u969c\u6765\u6ee1\u8db3\u5185\u5b58\u6a21\u578b\u7684\u89c4\u5219<\/p>\n

        \n
      1. volatile store\u524d\u63d2\u5165StoreStore\u5c4f\u969c<\/li>\n
      2. \u6240\u6709final\u5b57\u6bb5\u5199\u5165\u540e\u4f46\u5728\u6784\u9020\u5668\u8fd4\u56de\u524d\u63d2\u5165StoreStore<\/li>\n
      3. volatile store\u540e\u63d2\u5165StoreLoad\u5c4f\u969c<\/li>\n
      4. \u5728volatile load\u540e\u63d2\u5165LoadLoad\u548cLoadStore\u5c4f\u969c<\/li>\n
      5. monitor enter\u548cvolatile load\u89c4\u5219\u4e00\u81f4\uff0cmonitor exit \u548cvolatile store\u89c4\u5219\u4e00\u81f4\u3002<\/li>\n<\/ol>\n
        HappenBefore<\/strong><\/div>\n

        \u524d\u9762\u63d0\u5230\u7684\u5404\u79cd\u5185\u5b58\u5c4f\u969c\u5bf9\u5e94\u5f00\u53d1\u8005\u6765\u8bf4\u8fd8\u662f\u6bd4\u8f83\u590d\u6742\u5e95\u5c42\uff0c\u56e0\u6b64JMM\u53c8\u53ef\u4ee5\u4f7f\u7528\u4e00\u7cfb\u5217HappenBefore\u7684\u504f\u5e8f\u5173\u7cfb\u7684\u89c4\u5219\u65b9\u5f0f\u6765\u8bf4\u660e\uff0c\u8981\u60f3\u4fdd\u8bc1\u6267\u884c\u64cd\u4f5cB\u7684\u7ebf\u7a0b\u770b\u5230\u64cd\u4f5cA\u7684\u7ed3\u679c\uff08\u65e0\u8bbaA\u548cB\u662f\u5426\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u6267\u884c), \u90a3\u4e48\u5728A\u548cB\u4e4b\u95f4\u5fc5\u987b\u8981\u6ee1\u8db3HappenBefore\u5173\u7cfb\uff0c\u5426\u5219JVM\u53ef\u4ee5\u5bf9\u5b83\u4eec\u4efb\u610f\u91cd\u6392\u5e8f\u3002<\/p>\n

        HappenBefore\u89c4\u5219\u5217\u8868<\/strong><\/span><\/div>\n

        HappendBefore\u89c4\u5219\u5305\u62ec<\/p>\n

          \n
        1. \u7a0b\u5e8f\u987a\u5e8f\u89c4\u5219: \u5982\u679c\u7a0b\u5e8f\u4e2d\u64cd\u4f5cA\u5728\u64cd\u4f5cB\u4e4b\u524d\uff0c\u90a3\u4e48\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u64cd\u4f5cA\u5c06\u5728\u64cd\u4f5cB\u4e4b\u524d\u8fdb\u884c<\/li>\n
        2. \u76d1\u89c6\u5668\u9501\u89c4\u5219: \u5728\u76d1\u89c6\u5668\u9501\u4e0a\u7684\u9501\u64cd\u4f5c\u5fc5\u987b\u5728\u540c\u4e00\u4e2a\u76d1\u89c6\u5668\u9501\u4e0a\u7684\u52a0\u9501\u64cd\u4f5c\u4e4b\u524d\u6267\u884c<\/li>\n
        3. volatile\u53d8\u91cf\u89c4\u5219: volatile\u53d8\u91cf\u7684\u5199\u5165\u64cd\u4f5c\u5fc5\u987b\u5728\u8be5\u53d8\u91cf\u7684\u8bfb\u64cd\u4f5c\u4e4b\u524d\u6267\u884c<\/li>\n
        4. \u7ebf\u7a0b\u542f\u52a8\u89c4\u5219: \u5728\u7ebf\u7a0b\u4e0a\u5bf9Thread.start\u7684\u8c03\u7528\u5fc5\u987b\u5728\u8be5\u7ebf\u7a0b\u4e2d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u4e4b\u524d\u6267\u884c<\/li>\n
        5. \u7ebf\u7a0b\u7ed3\u675f\u89c4\u5219: \u7ebf\u7a0b\u4e2d\u7684\u4efb\u4f55\u64cd\u4f5c\u90fd\u5fc5\u987b\u5728\u5176\u4ed6\u7ebf\u7a0b\u68c0\u6d4b\u5230\u8be5\u7ebf\u7a0b\u5df2\u7ecf\u7ed3\u675f\u4e4b\u524d\u6267\u884c<\/li>\n
        6. \u4e2d\u65ad\u89c4\u5219: \u5f53\u4e00\u4e2a\u7ebf\u7a0b\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e0a\u8c03\u7528interrupt\u65f6\uff0c\u5fc5\u987b\u5728\u88ab\u4e2d\u65ad\u7ebf\u7a0b\u68c0\u6d4b\u5230interrupt\u4e4b\u524d\u6267\u884c<\/li>\n
        7. \u4f20\u9012\u6027: \u5982\u679c\u64cd\u4f5cA\u5728\u64cd\u4f5cB\u4e4b\u524d\u6267\u884c\uff0c\u5e76\u4e14\u64cd\u4f5cB\u5728\u64cd\u4f5cC\u4e4b\u524d\u6267\u884c\uff0c\u90a3\u4e48\u64cd\u4f5cA\u5728\u64cd\u4f5cC\u4e4b\u524d\u6267\u884c\u3002<\/li>\n<\/ol>\n

          \u5176\u4e2d\u663e\u793a\u9501\u4e0e\u76d1\u89c6\u5668\u9501\u6709\u76f8\u540c\u7684\u5185\u5b58\u8bed\u4e49\uff0c\u539f\u5b50\u53d8\u91cf\u4e0evolatile\u6709\u76f8\u540c\u7684\u5185\u5b58\u8bed\u4e49\u3002\u9501\u7684\u83b7\u53d6\u548c\u91ca\u653e\u3001volatile\u53d8\u91cf\u7684\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u6ee1\u8db3\u5168\u5e8f\u5173\u7cfb\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528volatile\u7684\u5199\u5165\u5728\u540e\u7eed\u7684volatile\u7684\u8bfb\u53d6\u4e4b\u524d\u8fdb\u884c\u3002<\/p>\n

          \u53ef\u4ee5\u5229\u7528\u4e0a\u8ff0HappenBefore\u7684\u591a\u4e2a\u89c4\u5219\u8fdb\u884c\u7ec4\u5408\u3002<\/p>\n

          \u4f8b\u5982\u7ebf\u7a0bA\u8fdb\u5165\u76d1\u89c6\u5668\u9501\u540e\uff0c\u5728\u91ca\u653e\u76d1\u89c6\u5668\u9501\u4e4b\u524d\u7684\u64cd\u4f5c\u6839\u636e\u7a0b\u5e8f\u987a\u5e8f\u89c4\u5219HappenBefore\u4e8e\u76d1\u89c6\u5668\u91ca\u653e\u64cd\u4f5c\uff0c\u800c\u76d1\u89c6\u5668\u91ca\u653e\u64cd\u4f5cHappenBefore\u4e8e\u540e\u7eed\u7684\u7ebf\u7a0bB\u7684\u5bf9\u76f8\u540c\u76d1\u89c6\u5668\u9501\u7684\u83b7\u53d6\u64cd\u4f5c\uff0c\u83b7\u53d6\u64cd\u4f5cHappenBefore\u4e0e\u7ebf\u7a0bB\u4e2d\u7684\u64cd\u4f5c\u3002<\/p>\n

          \u53c2\u8003<\/strong><\/div>\n