{"id":2109,"date":"2017-08-06T17:24:44","date_gmt":"2017-08-06T09:24:44","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2109"},"modified":"2026-08-18T12:09:38","modified_gmt":"2026-08-18T04:09:38","slug":"volatile%e5%85%b3%e9%94%ae%e5%ad%97%e5%bc%95%e5%8f%91%e7%9a%84%e7%9f%a5%e8%af%86%e7%82%b9","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2109","title":{"rendered":"volatile\u5173\u952e\u5b57\u53ca\u76f8\u540c\u77e5\u8bc6\u70b9"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u8981\u7406\u89e3 <strong style=\"color: #4870ac;\">volatile<\/strong> \u4e3a\u4ec0\u4e48\u80fd\u4fdd\u8bc1\u53ef\u89c1\u6027\uff0c\u5148\u770b Java \u5185\u5b58\u6a21\u578b\uff1a\u53d8\u91cf\u5728\u4e3b\u5185\u5b58\uff0c\u7ebf\u7a0b\u5728\u5de5\u4f5c\u5185\u5b58\u91cc\u64cd\u4f5c\u62f7\u8d1d\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u4e0d\u80fd\u76f4\u63a5\u8bfb\u5bf9\u65b9\u5de5\u4f5c\u5185\u5b58\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5728\u6b64\u6a21\u578b\u4e0b\u4f1a\u51fa\u73b0\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898\uff08\u4e24\u4e2a\u7ebf\u7a0b\u5bf9 i++ \u6700\u7ec8\u5f97\u5230 11 \u800c\u4e0d\u662f 12\uff09\u3002\u5e76\u53d1\u7f16\u7a0b\u8981\u540c\u65f6\u6293\u4f4f\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u3001\u6709\u5e8f\u6027\uff0c\u518d\u770b volatile \u80fd\u505a\u4ec0\u4e48\u3001\u4e0d\u80fd\u505a\u4ec0\u4e48\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u7279\u6027<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">synchronized<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">volatile<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u539f\u5b50\u6027<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u53ef\u4ee5<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e0d\u884c\uff08i++ \u4ecd\u4f1a\u4e22\u66f4\u65b0\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u53ef\u89c1\u6027<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u53ef\u4ee5\uff08\u89e3\u9501\u5199\u56de\u4e3b\u5b58\uff09<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u53ef\u4ee5\uff08\u6bcf\u6b21\u8bfb\u4e3b\u5b58\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6709\u5e8f\u6027<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u53ef\u4ee5<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7981\u6b62\u6307\u4ee4\u91cd\u6392\uff0chappens-before<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4ee3\u4ef7<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e92\u65a5\uff0c\u53ef\u80fd\u963b\u585e<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8f7b\u91cf\uff0c\u65e0\u9501<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001Java\u5185\u5b58\u6a21\u578b<\/h2>\n<p style=\"margin: 1em 0px;\">\u60f3\u8981\u7406\u89e3volatile\u4e3a\u4ec0\u4e48\u80fd\u786e\u4fdd\u53ef\u89c1\u6027\uff0c\u5c31\u8981\u5148\u7406\u89e3Java\u4e2d\u7684\u5185\u5b58\u6a21\u578b\u662f\u4ec0\u4e48\u6837\u7684\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Java\u5185\u5b58\u6a21\u578b\u89c4\u5b9a\u4e86\u6240\u6709\u7684\u53d8\u91cf\u90fd\u5b58\u50a8\u5728\u4e3b\u5185\u5b58\u4e2d\u3002\u6bcf\u6761\u7ebf\u7a0b\u4e2d\u8fd8\u6709\u81ea\u5df1\u7684\u5de5\u4f5c\u5185\u5b58\uff0c\u7ebf\u7a0b\u7684\u5de5\u4f5c\u5185\u5b58\u4e2d\u4fdd\u5b58\u4e86\u88ab\u8be5\u7ebf\u7a0b\u6240\u4f7f\u7528\u5230\u7684\u53d8\u91cf\uff08\u8fd9\u4e9b\u53d8\u91cf\u662f\u4ece\u4e3b\u5185\u5b58\u4e2d\u62f7\u8d1d\u800c\u6765\uff09\u3002\u7ebf\u7a0b\u5bf9\u53d8\u91cf\u7684\u6240\u6709\u64cd\u4f5c\uff08\u8bfb\u53d6\uff0c\u8d4b\u503c\uff09\u90fd\u5fc5\u987b\u5728\u5de5\u4f5c\u5185\u5b58\u4e2d\u8fdb\u884c\u3002\u4e0d\u540c\u7ebf\u7a0b\u4e4b\u95f4\u4e5f\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5bf9\u65b9\u5de5\u4f5c\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\uff0c\u7ebf\u7a0b\u95f4\u53d8\u91cf\u503c\u7684\u4f20\u9012\u5747\u9700\u8981\u901a\u8fc7\u4e3b\u5185\u5b58\u6765\u5b8c\u6210\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/upload-images.jianshu.io\/upload_images\/3985563-8a3d9a1b94b97e83.png?imageMogr2\/auto-orient\/strip%7CimageView2\/2\/w\/1240\" alt=\"\" data-original-src=\"http:\/\/upload-images.jianshu.io\/upload_images\/3985563-8a3d9a1b94b97e83.png?imageMogr2\/auto-orient\/strip%7CimageView2\/2\" \/><br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/08\/\u256c\u0432\u2568\u253c\u255c\u256a\u2550\u255d_20200415221755.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2830\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/08\/\u256c\u0432\u2568\u253c\u255c\u256a\u2550\u255d_20200415221755.png\" alt=\"\u256c\u0432\u2568\u253c\u255c\u256a\u2550\u255d_20200415221755\" width=\"646\" height=\"317\" \/><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u57fa\u4e8e\u6b64\u79cd\u5185\u5b58\u6a21\u578b\uff0c\u4fbf\u4ea7\u751f\u4e86\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u6570\u636e\u201c\u810f\u8bfb\u201d\u7b49\u95ee\u9898\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a\u5728java\u4e2d\uff0c\u6267\u884c\u4e0b\u9762\u8fd9\u4e2a\u8bed\u53e5\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">i\u00a0 = 10++;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6267\u884c\u7ebf\u7a0b\u5fc5\u987b\u5148\u5728\u81ea\u5df1\u7684\u5de5\u4f5c\u7ebf\u7a0b\u4e2d\u5bf9\u53d8\u91cfi\u6240\u5728\u7684\u7f13\u5b58\u884c\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\uff0c\u7136\u540e\u518d\u5199\u5165\u4e3b\u5b58\u5f53\u4e2d\u3002\u800c\u4e0d\u662f\u76f4\u63a5\u5c06\u6570\u503c10\u5199\u5165\u4e3b\u5b58\u5f53\u4e2d\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6bd4\u5982\u540c\u65f6\u67092\u4e2a\u7ebf\u7a0b\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5047\u5982\u521d\u59cb\u65f6i\u7684\u503c\u4e3a10\uff0c\u90a3\u4e48\u6211\u4eec\u5e0c\u671b\u4e24\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u4e4b\u540ei\u7684\u503c\u53d8\u4e3a12\u3002\u4f46\u662f\u4e8b\u5b9e\u4f1a\u662f\u8fd9\u6837\u5417\uff1f<\/p>\n<p style=\"margin: 1em 0px;\">\u53ef\u80fd\u5b58\u5728\u4e0b\u9762\u4e00\u79cd\u60c5\u51b5\uff1a\u521d\u59cb\u65f6\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u5206\u522b\u8bfb\u53d6i\u7684\u503c\u5b58\u5165\u5404\u81ea\u6240\u5728\u7684\u5de5\u4f5c\u5185\u5b58\u5f53\u4e2d\uff0c\u7136\u540e\u7ebf\u7a0b1\u8fdb\u884c\u52a01\u64cd\u4f5c\uff0c\u7136\u540e\u628ai\u7684\u6700\u65b0\u503c11\u5199\u5165\u5230\u5185\u5b58\u3002\u6b64\u65f6\u7ebf\u7a0b2\u7684\u5de5\u4f5c\u5185\u5b58\u5f53\u4e2di\u7684\u503c\u8fd8\u662f10\uff0c\u8fdb\u884c\u52a01\u64cd\u4f5c\u4e4b\u540e\uff0ci\u7684\u503c\u4e3a11\uff0c\u7136\u540e\u7ebf\u7a0b2\u628ai\u7684\u503c\u5199\u5165\u5185\u5b58\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6700\u7ec8\u7ed3\u679ci\u7684\u503c\u662f11\uff0c\u800c\u4e0d\u662f12\u3002\u8fd9\u5c31\u662f\u8457\u540d\u7684\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898\u3002\u901a\u5e38\u79f0\u8fd9\u79cd\u88ab\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u7684\u53d8\u91cf\u4e3a\u5171\u4eab\u53d8\u91cf\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\u5982\u4f55\u786e\u4fdd\u5171\u4eab\u53d8\u91cf\u5728\u591a\u7ebf\u7a0b\u8bbf\u95ee\u65f6\u80fd\u591f\u6b63\u786e\u8f93\u51fa\u7ed3\u679c\u5462\uff1f<\/p>\n<p style=\"margin: 1em 0px;\">\u5728\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u4e4b\u524d\uff0c\u6211\u4eec\u8981\u5148\u4e86\u89e3\u5e76\u53d1\u7f16\u7a0b\u7684\u4e09\u5927\u6982\u5ff5\uff1a\u539f\u5b50\u6027\uff0c\u6709\u5e8f\u6027\uff0c\u53ef\u89c1\u6027\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u539f\u5b50\u6027<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1.\u5b9a\u4e49<\/h3>\n<p style=\"margin: 1em 0px;\">\u539f\u5b50\u6027\uff1a\u5373\u4e00\u4e2a\u64cd\u4f5c\u6216\u8005\u591a\u4e2a\u64cd\u4f5c \u8981\u4e48\u5168\u90e8\u6267\u884c\u5e76\u4e14\u6267\u884c\u7684\u8fc7\u7a0b\u4e0d\u4f1a\u88ab\u4efb\u4f55\u56e0\u7d20\u6253\u65ad\uff0c\u8981\u4e48\u5c31\u90fd\u4e0d\u6267\u884c\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2.\u5b9e\u4f8b<\/h3>\n<p style=\"margin: 1em 0px;\">\u4e00\u4e2a\u5f88\u7ecf\u5178\u7684\u4f8b\u5b50\u5c31\u662f\u94f6\u884c\u8d26\u6237\u8f6c\u8d26\u95ee\u9898\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">\u6bd4\u5982\u4ece\u8d26\u6237A\u5411\u8d26\u6237B\u8f6c1000\u5143\uff0c\u90a3\u4e48\u5fc5\u7136\u5305\u62ec2\u4e2a\u64cd\u4f5c\uff1a\u4ece\u8d26\u6237A\u51cf\u53bb1000\u5143\uff0c\u5f80\u8d26\u6237B\u52a0\u4e0a1000\u5143\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u8bd5\u60f3\u4e00\u4e0b\uff0c\u5982\u679c\u8fd92\u4e2a\u64cd\u4f5c\u4e0d\u5177\u5907\u539f\u5b50\u6027\uff0c\u4f1a\u9020\u6210\u4ec0\u4e48\u6837\u7684\u540e\u679c\u3002\u5047\u5982\u4ece\u8d26\u6237A\u51cf\u53bb1000\u5143\u4e4b\u540e\uff0c\u64cd\u4f5c\u7a81\u7136\u4e2d\u6b62\u3002\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u8d26\u6237A\u867d\u7136\u51cf\u53bb\u4e861000\u5143\uff0c\u4f46\u662f\u8d26\u6237B\u6ca1\u6709\u6536\u5230\u8fd9\u4e2a\u8f6c\u8fc7\u6765\u76841000\u5143\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6240\u4ee5\u8fd92\u4e2a\u64cd\u4f5c\u5fc5\u987b\u8981\u5177\u5907\u539f\u5b50\u6027\u624d\u80fd\u4fdd\u8bc1\u4e0d\u51fa\u73b0\u4e00\u4e9b\u610f\u5916\u7684\u95ee\u9898\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u540c\u6837\u5730\u53cd\u6620\u5230\u5e76\u53d1\u7f16\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u4ec0\u4e48\u7ed3\u679c\u5462\uff1f<\/p>\n<p style=\"margin: 1em 0px;\">\u4e3e\u4e2a\u6700\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5927\u5bb6\u60f3\u4e00\u4e0b\u5047\u5982\u4e3a\u4e00\u4e2a32\u4f4d\u7684\u53d8\u91cf\u8d4b\u503c\u8fc7\u7a0b\u4e0d\u5177\u5907\u539f\u5b50\u6027\u7684\u8bdd\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u540e\u679c\uff1f<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">i = 9;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5047\u82e5\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5230\u8fd9\u4e2a\u8bed\u53e5\u65f6\uff0c\u6211\u6682\u4e14\u5047\u8bbe\u4e3a\u4e00\u4e2a32\u4f4d\u7684\u53d8\u91cf\u8d4b\u503c\u5305\u62ec\u4e24\u4e2a\u8fc7\u7a0b\uff1a\u4e3a\u4f4e16\u4f4d\u8d4b\u503c\uff0c\u4e3a\u9ad816\u4f4d\u8d4b\u503c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\u5c31\u53ef\u80fd\u53d1\u751f\u4e00\u79cd\u60c5\u51b5\uff1a\u5f53\u5c06\u4f4e16\u4f4d\u6570\u503c\u5199\u5165\u4e4b\u540e\uff0c\u7a81\u7136\u88ab\u4e2d\u65ad\uff0c\u800c\u6b64\u65f6\u53c8\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u8bfb\u53d6i\u7684\u503c\uff0c\u90a3\u4e48\u8bfb\u53d6\u5230\u7684\u5c31\u662f\u9519\u8bef\u7684\u6570\u636e\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3.Java\u4e2d\u7684\u539f\u5b50\u6027<\/h3>\n<p style=\"margin: 1em 0px;\">\u5728Java\u4e2d\uff0c\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u7684\u8bfb\u53d6\u548c\u8d4b\u503c\u64cd\u4f5c\u662f\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u5373\u8fd9\u4e9b\u64cd\u4f5c\u662f\u4e0d\u53ef\u88ab\u4e2d\u65ad\u7684\uff0c\u8981\u4e48\u6267\u884c\uff0c\u8981\u4e48\u4e0d\u6267\u884c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e0a\u9762\u4e00\u53e5\u8bdd\u867d\u7136\u770b\u8d77\u6765\u7b80\u5355\uff0c\u4f46\u662f\u7406\u89e3\u8d77\u6765\u5e76\u4e0d\u662f\u90a3\u4e48\u5bb9\u6613\u3002\u770b\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50i\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">\u8bf7\u5206\u6790\u4ee5\u4e0b\u54ea\u4e9b\u64cd\u4f5c\u662f\u539f\u5b50\u6027\u64cd\u4f5c\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">x = 10;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e51\ny = x;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e52\nx++;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e53\nx = x + 1;\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e54<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u548b\u4e00\u770b\uff0c\u53ef\u80fd\u4f1a\u8bf4\u4e0a\u9762\u76844\u4e2a\u8bed\u53e5\u4e2d\u7684\u64cd\u4f5c\u90fd\u662f\u539f\u5b50\u6027\u64cd\u4f5c\u3002\u5176\u5b9e\u53ea\u6709\u8bed\u53e51\u662f\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u5176\u4ed6\u4e09\u4e2a\u8bed\u53e5\u90fd\u4e0d\u662f\u539f\u5b50\u6027\u64cd\u4f5c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u8bed\u53e51\u662f\u76f4\u63a5\u5c06\u6570\u503c10\u8d4b\u503c\u7ed9x\uff0c\u4e5f\u5c31\u662f\u8bf4\u7ebf\u7a0b\u6267\u884c\u8fd9\u4e2a\u8bed\u53e5\u7684\u4f1a\u76f4\u63a5\u5c06\u6570\u503c10\u5199\u5165\u5230\u5de5\u4f5c\u5185\u5b58\u4e2d\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u8bed\u53e52\u5b9e\u9645\u4e0a\u5305\u542b2\u4e2a\u64cd\u4f5c\uff0c\u5b83\u5148\u8981\u53bb\u8bfb\u53d6x\u7684\u503c\uff0c\u518d\u5c06x\u7684\u503c\u5199\u5165\u5de5\u4f5c\u5185\u5b58\uff0c\u867d\u7136\u8bfb\u53d6x\u7684\u503c\u4ee5\u53ca \u5c06x\u7684\u503c\u5199\u5165\u5de5\u4f5c\u5185\u5b58 \u8fd92\u4e2a\u64cd\u4f5c\u90fd\u662f\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u4f46\u662f\u5408\u8d77\u6765\u5c31\u4e0d\u662f\u539f\u5b50\u6027\u64cd\u4f5c\u4e86\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u540c\u6837\u7684\uff0cx++\u548c x = x+1\u5305\u62ec3\u4e2a\u64cd\u4f5c\uff1a\u8bfb\u53d6x\u7684\u503c\uff0c\u8fdb\u884c\u52a01\u64cd\u4f5c\uff0c\u5199\u5165\u65b0\u7684\u503c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6240\u4ee5\u4e0a\u97624\u4e2a\u8bed\u53e5\u53ea\u6709\u8bed\u53e51\u7684\u64cd\u4f5c\u5177\u5907\u539f\u5b50\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u6709\u7b80\u5355\u7684\u8bfb\u53d6\u3001\u8d4b\u503c\uff08\u800c\u4e14\u5fc5\u987b\u662f\u5c06\u6570\u5b57\u8d4b\u503c\u7ed9\u67d0\u4e2a\u53d8\u91cf\uff0c\u53d8\u91cf\u4e4b\u95f4\u7684\u76f8\u4e92\u8d4b\u503c\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff09\u624d\u662f\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa\uff0cJava\u5185\u5b58\u6a21\u578b\u53ea\u4fdd\u8bc1\u4e86\u57fa\u672c\u8bfb\u53d6\u548c\u8d4b\u503c\u662f\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u5982\u679c\u8981\u5b9e\u73b0\u66f4\u5927\u8303\u56f4\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7synchronized\u548cLock\u6765\u5b9e\u73b0\u3002\u7531\u4e8esynchronized\u548cLock\u80fd\u591f\u4fdd\u8bc1\u4efb\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u8be5\u4ee3\u7801\u5757\uff0c\u90a3\u4e48\u81ea\u7136\u5c31\u4e0d\u5b58\u5728\u539f\u5b50\u6027\u95ee\u9898\u4e86\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u539f\u5b50\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5173\u4e8esynchronized\u548cLock\u7684\u4f7f\u7528\uff0c\u53c2\u8003\uff1a<a class=\"external\" href=\"http:\/\/www.jianshu.com\/p\/96c89e6e7e90\" target=\"_blank\" rel=\"nofollow noopener\">\u5173\u4e8esynchronized\u548cReentrantLock\u4e4b\u591a\u7ebf\u7a0b\u540c\u6b65\u8be6\u89e3<\/a><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u53ef\u89c1\u6027<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1.\u5b9a\u4e49<\/h3>\n<p style=\"margin: 1em 0px;\">\u53ef\u89c1\u6027\u662f\u6307\u5f53\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u4e86\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5176\u4ed6\u7ebf\u7a0b\u80fd\u591f\u7acb\u5373\u770b\u5f97\u5230\u4fee\u6539\u7684\u503c\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2.\u5b9e\u4f8b<\/h3>\n<p style=\"margin: 1em 0px;\">\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/\u7ebf\u7a0b1\u6267\u884c\u7684\u4ee3\u7801\nint i = 0;\ni = 10;\n\n\/\/\u7ebf\u7a0b2\u6267\u884c\u7684\u4ee3\u7801\nj = i;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7531\u4e0a\u9762\u7684\u5206\u6790\u53ef\u77e5\uff0c\u5f53\u7ebf\u7a0b1\u6267\u884c i =10\u8fd9\u53e5\u65f6\uff0c\u4f1a\u5148\u628ai\u7684\u521d\u59cb\u503c\u52a0\u8f7d\u5230\u5de5\u4f5c\u5185\u5b58\u4e2d\uff0c\u7136\u540e\u8d4b\u503c\u4e3a10\uff0c\u90a3\u4e48\u5728\u7ebf\u7a0b1\u7684\u5de5\u4f5c\u5185\u5b58\u5f53\u4e2di\u7684\u503c\u53d8\u4e3a10\u4e86\uff0c\u5374\u6ca1\u6709\u7acb\u5373\u5199\u5165\u5230\u4e3b\u5b58\u5f53\u4e2d\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6b64\u65f6\u7ebf\u7a0b2\u6267\u884c j = i\uff0c\u5b83\u4f1a\u5148\u53bb\u4e3b\u5b58\u8bfb\u53d6i\u7684\u503c\u5e76\u52a0\u8f7d\u5230\u7ebf\u7a0b2\u7684\u5de5\u4f5c\u5185\u5b58\u5f53\u4e2d\uff0c\u6ce8\u610f\u6b64\u65f6\u5185\u5b58\u5f53\u4e2di\u7684\u503c\u8fd8\u662f0\uff0c\u90a3\u4e48\u5c31\u4f1a\u4f7f\u5f97j\u7684\u503c\u4e3a0\uff0c\u800c\u4e0d\u662f10.<\/p>\n<p style=\"margin: 1em 0px;\">\u8fd9\u5c31\u662f\u53ef\u89c1\u6027\u95ee\u9898\uff0c\u7ebf\u7a0b1\u5bf9\u53d8\u91cfi\u4fee\u6539\u4e86\u4e4b\u540e\uff0c\u7ebf\u7a0b2\u6ca1\u6709\u7acb\u5373\u770b\u5230\u7ebf\u7a0b1\u4fee\u6539\u7684\u503c\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3.Java\u4e2d\u7684\u53ef\u89c1\u6027<\/h3>\n<p style=\"margin: 1em 0px;\">\u5bf9\u4e8e\u53ef\u89c1\u6027\uff0cJava\u63d0\u4f9b\u4e86volatile\u5173\u952e\u5b57\u6765\u4fdd\u8bc1\u53ef\u89c1\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5f53\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf\u88abvolatile\u4fee\u9970\u65f6\uff0c\u5b83\u4f1a\u4fdd\u8bc1\u4fee\u6539\u7684\u503c\u4f1a\u7acb\u5373\u88ab\u66f4\u65b0\u5230\u4e3b\u5b58\uff0c\u5f53\u6709\u5176\u4ed6\u7ebf\u7a0b\u9700\u8981\u8bfb\u53d6\u65f6\uff0c\u5b83\u4f1a\u53bb\u5185\u5b58\u4e2d\u8bfb\u53d6\u65b0\u503c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u800c\u666e\u901a\u7684\u5171\u4eab\u53d8\u91cf\u4e0d\u80fd\u4fdd\u8bc1\u53ef\u89c1\u6027\uff0c\u56e0\u4e3a\u666e\u901a\u5171\u4eab\u53d8\u91cf\u88ab\u4fee\u6539\u4e4b\u540e\uff0c\u4ec0\u4e48\u65f6\u5019\u88ab\u5199\u5165\u4e3b\u5b58\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u5f53\u5176\u4ed6\u7ebf\u7a0b\u53bb\u8bfb\u53d6\u65f6\uff0c\u6b64\u65f6\u5185\u5b58\u4e2d\u53ef\u80fd\u8fd8\u662f\u539f\u6765\u7684\u65e7\u503c\uff0c\u56e0\u6b64\u65e0\u6cd5\u4fdd\u8bc1\u53ef\u89c1\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u53e6\u5916\uff0c\u901a\u8fc7synchronized\u548cLock\u4e5f\u80fd\u591f\u4fdd\u8bc1\u53ef\u89c1\u6027\uff0csynchronized\u548cLock\u80fd\u4fdd\u8bc1\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u9501\u7136\u540e\u6267\u884c\u540c\u6b65\u4ee3\u7801\uff0c\u5e76\u4e14\u5728\u91ca\u653e\u9501\u4e4b\u524d\u4f1a\u5c06\u5bf9\u53d8\u91cf\u7684\u4fee\u6539\u5237\u65b0\u5230\u4e3b\u5b58\u5f53\u4e2d\u3002\u56e0\u6b64\u53ef\u4ee5\u4fdd\u8bc1\u53ef\u89c1\u6027\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001\u6709\u5e8f\u6027<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1.\u5b9a\u4e49<\/h3>\n<p style=\"margin: 1em 0px;\">\u6709\u5e8f\u6027\uff1a\u5373\u7a0b\u5e8f\u6267\u884c\u7684\u987a\u5e8f\u6309\u7167\u4ee3\u7801\u7684\u5148\u540e\u987a\u5e8f\u6267\u884c\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2.\u5b9e\u4f8b<\/h3>\n<p style=\"margin: 1em 0px;\">\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">int i = 0;\n\nboolean flag = false;\n\ni = 1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e51\nflag = true;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e52<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e0a\u9762\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2aint\u578b\u53d8\u91cf\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2aboolean\u7c7b\u578b\u53d8\u91cf\uff0c\u7136\u540e\u5206\u522b\u5bf9\u4e24\u4e2a\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\u3002\u4ece\u4ee3\u7801\u987a\u5e8f\u4e0a\u770b\uff0c\u8bed\u53e51\u662f\u5728\u8bed\u53e52\u524d\u9762\u7684\uff0c\u90a3\u4e48JVM\u5728\u771f\u6b63\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\u7684\u65f6\u5019\u4f1a\u4fdd\u8bc1\u8bed\u53e51\u4e00\u5b9a\u4f1a\u5728\u8bed\u53e52\u524d\u9762\u6267\u884c\u5417\uff1f\u4e0d\u4e00\u5b9a\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f\u8fd9\u91cc\u53ef\u80fd\u4f1a\u53d1\u751f\u6307\u4ee4\u91cd\u6392\u5e8f\uff08Instruction Reorder\uff09\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u89e3\u91ca\u4e00\u4e0b\u4ec0\u4e48\u662f\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u4e00\u822c\u6765\u8bf4\uff0c\u5904\u7406\u5668\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\uff0c\u53ef\u80fd\u4f1a\u5bf9\u8f93\u5165\u4ee3\u7801\u8fdb\u884c\u4f18\u5316\uff0c\u5b83\u4e0d\u4fdd\u8bc1\u7a0b\u5e8f\u4e2d\u5404\u4e2a\u8bed\u53e5\u7684\u6267\u884c\u5148\u540e\u987a\u5e8f\u540c\u4ee3\u7801\u4e2d\u7684\u987a\u5e8f\u4e00\u81f4\uff0c\u4f46\u662f\u5b83\u4f1a\u4fdd\u8bc1\u7a0b\u5e8f\u6700\u7ec8\u6267\u884c\u7ed3\u679c\u548c\u4ee3\u7801\u987a\u5e8f\u6267\u884c\u7684\u7ed3\u679c\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6bd4\u5982\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u8bed\u53e51\u548c\u8bed\u53e52\u8c01\u5148\u6267\u884c\u5bf9\u6700\u7ec8\u7684\u7a0b\u5e8f\u7ed3\u679c\u5e76\u6ca1\u6709\u5f71\u54cd\uff0c\u90a3\u4e48\u5c31\u6709\u53ef\u80fd\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u8bed\u53e52\u5148\u6267\u884c\u800c\u8bed\u53e51\u540e\u6267\u884c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u867d\u7136\u5904\u7406\u5668\u4f1a\u5bf9\u6307\u4ee4\u8fdb\u884c\u91cd\u6392\u5e8f\uff0c\u4f46\u662f\u5b83\u4f1a\u4fdd\u8bc1\u7a0b\u5e8f\u6700\u7ec8\u7ed3\u679c\u4f1a\u548c\u4ee3\u7801\u987a\u5e8f\u6267\u884c\u7ed3\u679c\u76f8\u540c\uff0c\u90a3\u4e48\u5b83\u9760\u4ec0\u4e48\u4fdd\u8bc1\u7684\u5462\uff1f\u518d\u770b\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">int a = 10;\u00a0\u00a0\u00a0 \/\/\u8bed\u53e51\nint r = 2;\u00a0\u00a0\u00a0 \/\/\u8bed\u53e52\na = a + 3;\u00a0\u00a0\u00a0 \/\/\u8bed\u53e53\nr = a*a;\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e54<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8fd9\u6bb5\u4ee3\u7801\u67094\u4e2a\u8bed\u53e5\uff0c\u90a3\u4e48\u53ef\u80fd\u7684\u4e00\u4e2a\u6267\u884c\u987a\u5e8f\u662f\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/upload-images.jianshu.io\/upload_images\/3985563-dcefcb1390cb3ed8.jpg?imageMogr2\/auto-orient\/strip%7CimageView2\/2\/w\/1240\" alt=\"\" data-original-src=\"http:\/\/upload-images.jianshu.io\/upload_images\/3985563-dcefcb1390cb3ed8.jpg?imageMogr2\/auto-orient\/strip%7CimageView2\/2\" \/><\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\u53ef\u4e0d\u53ef\u80fd\u662f\u8fd9\u4e2a\u6267\u884c\u987a\u5e8f\u5462\uff1a \u8bed\u53e52 \u8bed\u53e51 \u8bed\u53e54 \u8bed\u53e53<\/p>\n<p style=\"margin: 1em 0px;\">\u4e0d\u53ef\u80fd\uff0c\u56e0\u4e3a\u5904\u7406\u5668\u5728\u8fdb\u884c\u91cd\u6392\u5e8f\u65f6\u662f\u4f1a\u8003\u8651\u6307\u4ee4\u4e4b\u95f4\u7684\u6570\u636e\u4f9d\u8d56\u6027\uff0c\u5982\u679c\u4e00\u4e2a\u6307\u4ee4Instruction 2\u5fc5\u987b\u7528\u5230Instruction 1\u7684\u7ed3\u679c\uff0c\u90a3\u4e48\u5904\u7406\u5668\u4f1a\u4fdd\u8bc1Instruction 1\u4f1a\u5728Instruction 2\u4e4b\u524d\u6267\u884c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u867d\u7136\u91cd\u6392\u5e8f\u4e0d\u4f1a\u5f71\u54cd\u5355\u4e2a\u7ebf\u7a0b\u5185\u7a0b\u5e8f\u6267\u884c\u7684\u7ed3\u679c\uff0c\u4f46\u662f\u591a\u7ebf\u7a0b\u5462\uff1f\u4e0b\u9762\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/\u7ebf\u7a0b1:\n\ncontext = loadContext();\u00a0\u00a0 \/\/\u8bed\u53e51\ninited = true;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e52\n\n\u00a0\/\/\u7ebf\u7a0b2:\nwhile(!inited ){\n\u00a0\u00a0\u00a0sleep()\n}\ndoSomethingwithconfig(context);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u7531\u4e8e\u8bed\u53e51\u548c\u8bed\u53e52\u6ca1\u6709\u6570\u636e\u4f9d\u8d56\u6027\uff0c\u56e0\u6b64\u53ef\u80fd\u4f1a\u88ab\u91cd\u6392\u5e8f\u3002\u5047\u5982\u53d1\u751f\u4e86\u91cd\u6392\u5e8f\uff0c\u5728\u7ebf\u7a0b1\u6267\u884c\u8fc7\u7a0b\u4e2d\u5148\u6267\u884c\u8bed\u53e52\uff0c\u800c\u6b64\u662f\u7ebf\u7a0b2\u4f1a\u4ee5\u4e3a\u521d\u59cb\u5316\u5de5\u4f5c\u5df2\u7ecf\u5b8c\u6210\uff0c\u90a3\u4e48\u5c31\u4f1a\u8df3\u51fawhile\u5faa\u73af\uff0c\u53bb\u6267\u884cdoSomethingwithconfig(context)\u65b9\u6cd5\uff0c\u800c\u6b64\u65f6context\u5e76\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff0c\u5c31\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u9519\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa\uff0c\u6307\u4ee4\u91cd\u6392\u5e8f\u4e0d\u4f1a\u5f71\u54cd\u5355\u4e2a\u7ebf\u7a0b\u7684\u6267\u884c\uff0c\u4f46\u662f\u4f1a\u5f71\u54cd\u5230\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e5f\u5c31\u662f\u8bf4\uff0c\u8981\u60f3\u5e76\u53d1\u7a0b\u5e8f\u6b63\u786e\u5730\u6267\u884c\uff0c\u5fc5\u987b\u8981\u4fdd\u8bc1\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u4ee5\u53ca\u6709\u5e8f\u6027\u3002\u53ea\u8981\u6709\u4e00\u4e2a\u6ca1\u6709\u88ab\u4fdd\u8bc1\uff0c\u5c31\u6709\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u8fd0\u884c\u4e0d\u6b63\u786e\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3.Java\u4e2d\u7684\u6709\u5e8f\u6027<\/h3>\n<p style=\"margin: 1em 0px;\">\u5728Java\u5185\u5b58\u6a21\u578b\u4e2d\uff0c\u5141\u8bb8\u7f16\u8bd1\u5668\u548c\u5904\u7406\u5668\u5bf9\u6307\u4ee4\u8fdb\u884c\u91cd\u6392\u5e8f\uff0c\u4f46\u662f\u91cd\u6392\u5e8f\u8fc7\u7a0b\u4e0d\u4f1a\u5f71\u54cd\u5230\u5355\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u5374\u4f1a\u5f71\u54cd\u5230\u591a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5728Java\u91cc\u9762\uff0c\u53ef\u4ee5\u901a\u8fc7volatile\u5173\u952e\u5b57\u6765\u4fdd\u8bc1\u4e00\u5b9a\u7684\u201c\u6709\u5e8f\u6027\u201d\u3002\u53e6\u5916\u53ef\u4ee5\u901a\u8fc7synchronized\u548cLock\u6765\u4fdd\u8bc1\u6709\u5e8f\u6027\uff0c\u5f88\u663e\u7136\uff0csynchronized\u548cLock\u4fdd\u8bc1\u6bcf\u4e2a\u65f6\u523b\u662f\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u540c\u6b65\u4ee3\u7801\uff0c\u76f8\u5f53\u4e8e\u662f\u8ba9\u7ebf\u7a0b\u987a\u5e8f\u6267\u884c\u540c\u6b65\u4ee3\u7801\uff0c\u81ea\u7136\u5c31\u4fdd\u8bc1\u4e86\u6709\u5e8f\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u53e6\u5916\uff0cJava\u5185\u5b58\u6a21\u578b\u5177\u5907\u4e00\u4e9b\u5148\u5929\u7684\u201c\u6709\u5e8f\u6027\u201d\uff0c\u5373\u4e0d\u9700\u8981\u901a\u8fc7\u4efb\u4f55\u624b\u6bb5\u5c31\u80fd\u591f\u5f97\u5230\u4fdd\u8bc1\u7684\u6709\u5e8f\u6027\uff0c\u8fd9\u4e2a\u901a\u5e38\u4e5f\u79f0\u4e3a happens-before \u539f\u5219\u3002\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u7684\u6267\u884c\u6b21\u5e8f\u65e0\u6cd5\u4ecehappens-before\u539f\u5219\u63a8\u5bfc\u51fa\u6765\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u4e0d\u80fd\u4fdd\u8bc1\u5b83\u4eec\u7684\u6709\u5e8f\u6027\uff0c\u865a\u62df\u673a\u53ef\u4ee5\u968f\u610f\u5730\u5bf9\u5b83\u4eec\u8fdb\u884c\u91cd\u6392\u5e8f\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u4e0b\u9762\u5c31\u6765\u5177\u4f53\u4ecb\u7ecd\u4e0bhappens-before\u539f\u5219\uff08\u5148\u884c\u53d1\u751f\u539f\u5219\uff09\uff1a<\/h3>\n<p style=\"margin: 1em 0px;\">\u2460\u7a0b\u5e8f\u6b21\u5e8f\u89c4\u5219\uff1a\u4e00\u4e2a\u7ebf\u7a0b\u5185\uff0c\u6309\u7167\u4ee3\u7801\u987a\u5e8f\uff0c\u4e66\u5199\u5728\u524d\u9762\u7684\u64cd\u4f5c\u5148\u884c\u53d1\u751f\u4e8e\u4e66\u5199\u5728\u540e\u9762\u7684\u64cd\u4f5c<\/p>\n<p style=\"margin: 1em 0px;\">\u2461\u9501\u5b9a\u89c4\u5219\uff1a\u4e00\u4e2aunLock\u64cd\u4f5c\u5148\u884c\u53d1\u751f\u4e8e\u540e\u9762\u5bf9\u540c\u4e00\u4e2a\u9501\u7684lock\u64cd\u4f5c<\/p>\n<p style=\"margin: 1em 0px;\">\u2462volatile\u53d8\u91cf\u89c4\u5219\uff1a\u5bf9\u4e00\u4e2a\u53d8\u91cf\u7684\u5199\u64cd\u4f5c\u5148\u884c\u53d1\u751f\u4e8e\u540e\u9762\u5bf9\u8fd9\u4e2a\u53d8\u91cf\u7684\u8bfb\u64cd\u4f5c<\/p>\n<p style=\"margin: 1em 0px;\">\u2463\u4f20\u9012\u89c4\u5219\uff1a\u5982\u679c\u64cd\u4f5cA\u5148\u884c\u53d1\u751f\u4e8e\u64cd\u4f5cB\uff0c\u800c\u64cd\u4f5cB\u53c8\u5148\u884c\u53d1\u751f\u4e8e\u64cd\u4f5cC\uff0c\u5219\u53ef\u4ee5\u5f97\u51fa\u64cd\u4f5cA\u5148\u884c\u53d1\u751f\u4e8e\u64cd\u4f5cC<\/p>\n<p style=\"margin: 1em 0px;\">\u2464\u7ebf\u7a0b\u542f\u52a8\u89c4\u5219\uff1aThread\u5bf9\u8c61\u7684start()\u65b9\u6cd5\u5148\u884c\u53d1\u751f\u4e8e\u6b64\u7ebf\u7a0b\u7684\u6bcf\u4e2a\u4e00\u4e2a\u52a8\u4f5c<\/p>\n<p style=\"margin: 1em 0px;\">\u2465\u7ebf\u7a0b\u4e2d\u65ad\u89c4\u5219\uff1a\u5bf9\u7ebf\u7a0binterrupt()\u65b9\u6cd5\u7684\u8c03\u7528\u5148\u884c\u53d1\u751f\u4e8e\u88ab\u4e2d\u65ad\u7ebf\u7a0b\u7684\u4ee3\u7801\u68c0\u6d4b\u5230\u4e2d\u65ad\u4e8b\u4ef6\u7684\u53d1\u751f<\/p>\n<p style=\"margin: 1em 0px;\">\u2466\u7ebf\u7a0b\u7ec8\u7ed3\u89c4\u5219\uff1a\u7ebf\u7a0b\u4e2d\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u5148\u884c\u53d1\u751f\u4e8e\u7ebf\u7a0b\u7684\u7ec8\u6b62\u68c0\u6d4b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Thread.join()\u65b9\u6cd5\u7ed3\u675f\u3001Thread.isAlive()\u7684\u8fd4\u56de\u503c\u624b\u6bb5\u68c0\u6d4b\u5230\u7ebf\u7a0b\u5df2\u7ecf\u7ec8\u6b62\u6267\u884c<\/p>\n<p style=\"margin: 1em 0px;\">\u2467\u5bf9\u8c61\u7ec8\u7ed3\u89c4\u5219\uff1a\u4e00\u4e2a\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u5b8c\u6210\u5148\u884c\u53d1\u751f\u4e8e\u4ed6\u7684finalize()\u65b9\u6cd5\u7684\u5f00\u59cb<\/p>\n<p style=\"margin: 1em 0px;\">\u8fd98\u6761\u89c4\u5219\u4e2d\uff0c\u524d4\u6761\u89c4\u5219\u662f\u6bd4\u8f83\u91cd\u8981\u7684\uff0c\u540e4\u6761\u89c4\u5219\u90fd\u662f\u663e\u800c\u6613\u89c1\u7684\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u6211\u4eec\u6765\u89e3\u91ca\u4e00\u4e0b\u524d4\u6761\u89c4\u5219\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">\u5bf9\u4e8e\u7a0b\u5e8f\u6b21\u5e8f\u89c4\u5219\u6765\u8bf4\uff0c\u5c31\u662f\u4e00\u6bb5\u7a0b\u5e8f\u4ee3\u7801\u7684\u6267\u884c\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u770b\u8d77\u6765\u662f\u6709\u5e8f\u7684\u3002\u6ce8\u610f\uff0c\u867d\u7136\u8fd9\u6761\u89c4\u5219\u4e2d\u63d0\u5230\u201c\u4e66\u5199\u5728\u524d\u9762\u7684\u64cd\u4f5c\u5148\u884c\u53d1\u751f\u4e8e\u4e66\u5199\u5728\u540e\u9762\u7684\u64cd\u4f5c\u201d\uff0c\u8fd9\u4e2a\u5e94\u8be5\u662f\u7a0b\u5e8f\u770b\u8d77\u6765\u6267\u884c\u7684\u987a\u5e8f\u662f\u6309\u7167\u4ee3\u7801\u987a\u5e8f\u6267\u884c\u7684\uff0c\u4f46\u662f\u865a\u62df\u673a\u53ef\u80fd\u4f1a\u5bf9\u7a0b\u5e8f\u4ee3\u7801\u8fdb\u884c\u6307\u4ee4\u91cd\u6392\u5e8f\u3002\u867d\u7136\u8fdb\u884c\u91cd\u6392\u5e8f\uff0c\u4f46\u662f\u6700\u7ec8\u6267\u884c\u7684\u7ed3\u679c\u662f\u4e0e\u7a0b\u5e8f\u987a\u5e8f\u6267\u884c\u7684\u7ed3\u679c\u4e00\u81f4\u7684\uff0c\u5b83\u53ea\u4f1a\u5bf9\u4e0d\u5b58\u5728\u6570\u636e\u4f9d\u8d56\u6027\u7684\u6307\u4ee4\u8fdb\u884c\u91cd\u6392\u5e8f\u3002\u56e0\u6b64\uff0c\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\uff0c\u7a0b\u5e8f\u6267\u884c\u770b\u8d77\u6765\u662f\u6709\u5e8f\u6267\u884c\u7684\uff0c\u8fd9\u4e00\u70b9\u8981\u6ce8\u610f\u7406\u89e3\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u89c4\u5219\u662f\u7528\u6765\u4fdd\u8bc1\u7a0b\u5e8f\u5728\u5355\u7ebf\u7a0b\u4e2d\u6267\u884c\u7ed3\u679c\u7684\u6b63\u786e\u6027\uff0c\u4f46\u65e0\u6cd5\u4fdd\u8bc1\u7a0b\u5e8f\u5728\u591a\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e8c\u6761\u89c4\u5219\u4e5f\u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\uff0c\u4e5f\u5c31\u662f\u8bf4\u65e0\u8bba\u5728\u5355\u7ebf\u7a0b\u4e2d\u8fd8\u662f\u591a\u7ebf\u7a0b\u4e2d\uff0c\u540c\u4e00\u4e2a\u9501\u5982\u679c\u5904\u4e8e\u88ab\u9501\u5b9a\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u5fc5\u987b\u5148\u5bf9\u9501\u8fdb\u884c\u4e86\u91ca\u653e\u64cd\u4f5c\uff0c\u540e\u9762\u624d\u80fd\u7ee7\u7eed\u8fdb\u884clock\u64cd\u4f5c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e09\u6761\u89c4\u5219\u662f\u4e00\u6761\u6bd4\u8f83\u91cd\u8981\u7684\u89c4\u5219\u3002\u76f4\u89c2\u5730\u89e3\u91ca\u5c31\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u5148\u53bb\u5199\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u8fdb\u884c\u8bfb\u53d6\uff0c\u90a3\u4e48\u5199\u5165\u64cd\u4f5c\u80af\u5b9a\u4f1a\u5148\u884c\u53d1\u751f\u4e8e\u8bfb\u64cd\u4f5c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7b2c\u56db\u6761\u89c4\u5219\u5b9e\u9645\u4e0a\u5c31\u662f\u4f53\u73b0happens-before\u539f\u5219\u5177\u5907\u4f20\u9012\u6027\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e94\u3001\u6df1\u5165\u7406\u89e3volatile\u5173\u952e\u5b57<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1.volatile\u4fdd\u8bc1\u53ef\u89c1\u6027<\/h3>\n<p style=\"margin: 1em 0px;\">\u4e00\u65e6\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf\uff08\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u3001\u7c7b\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\uff09\u88abvolatile\u4fee\u9970\u4e4b\u540e\uff0c\u90a3\u4e48\u5c31\u5177\u5907\u4e86\u4e24\u5c42\u8bed\u4e49\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1\uff09\u4fdd\u8bc1\u4e86\u4e0d\u540c\u7ebf\u7a0b\u5bf9\u8fd9\u4e2a\u53d8\u91cf\u8fdb\u884c\u64cd\u4f5c\u65f6\u7684\u53ef\u89c1\u6027\uff0c\u5373\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u4e86\u67d0\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u8fd9\u65b0\u503c\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u6765\u8bf4\u662f\u7acb\u5373\u53ef\u89c1\u7684\u3002<\/p>\n<p style=\"margin: 1em 0px;\">2\uff09\u7981\u6b62\u8fdb\u884c\u6307\u4ee4\u91cd\u6392\u5e8f\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5148\u770b\u4e00\u6bb5\u4ee3\u7801\uff0c\u5047\u5982\u7ebf\u7a0b1\u5148\u6267\u884c\uff0c\u7ebf\u7a0b2\u540e\u6267\u884c\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/\u7ebf\u7a0b1\nboolean stop = false;\nwhile(!stop){\n\u00a0\u00a0\u00a0\u00a0doSomething();\n}\n\n\/\/\u7ebf\u7a0b2\nstop = true;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8fd9\u6bb5\u4ee3\u7801\u662f\u5f88\u5178\u578b\u7684\u4e00\u6bb5\u4ee3\u7801\uff0c\u5f88\u591a\u4eba\u5728\u4e2d\u65ad\u7ebf\u7a0b\u65f6\u53ef\u80fd\u90fd\u4f1a\u91c7\u7528\u8fd9\u79cd\u6807\u8bb0\u529e\u6cd5\u3002\u4f46\u662f\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5b8c\u5168\u8fd0\u884c\u6b63\u786e\u4e48\uff1f\u5373\u4e00\u5b9a\u4f1a\u5c06\u7ebf\u7a0b\u4e2d\u65ad\u4e48\uff1f\u4e0d\u4e00\u5b9a\uff0c\u4e5f\u8bb8\u5728\u5927\u591a\u6570\u65f6\u5019\uff0c\u8fd9\u4e2a\u4ee3\u7801\u80fd\u591f\u628a\u7ebf\u7a0b\u4e2d\u65ad\uff0c\u4f46\u662f\u4e5f\u6709\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65e0\u6cd5\u4e2d\u65ad\u7ebf\u7a0b\uff08\u867d\u7136\u8fd9\u4e2a\u53ef\u80fd\u6027\u5f88\u5c0f\uff0c\u4f46\u662f\u53ea\u8981\u4e00\u65e6\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u5c31\u4f1a\u9020\u6210\u6b7b\u5faa\u73af\u4e86\uff09\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u89e3\u91ca\u4e00\u4e0b\u8fd9\u6bb5\u4ee3\u7801\u4e3a\u4f55\u6709\u53ef\u80fd\u5bfc\u81f4\u65e0\u6cd5\u4e2d\u65ad\u7ebf\u7a0b\u3002\u5728\u524d\u9762\u5df2\u7ecf\u89e3\u91ca\u8fc7\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u90fd\u6709\u81ea\u5df1\u7684\u5de5\u4f5c\u5185\u5b58\uff0c\u90a3\u4e48\u7ebf\u7a0b1\u5728\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u4f1a\u5c06stop\u53d8\u91cf\u7684\u503c\u62f7\u8d1d\u4e00\u4efd\u653e\u5728\u81ea\u5df1\u7684\u5de5\u4f5c\u5185\u5b58\u5f53\u4e2d\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\u5f53\u7ebf\u7a0b2\u66f4\u6539\u4e86stop\u53d8\u91cf\u7684\u503c\u4e4b\u540e\uff0c\u4f46\u662f\u8fd8\u6ca1\u6765\u5f97\u53ca\u5199\u5165\u4e3b\u5b58\u5f53\u4e2d\uff0c\u7ebf\u7a0b2\u8f6c\u53bb\u505a\u5176\u4ed6\u4e8b\u60c5\u4e86\uff0c\u90a3\u4e48\u7ebf\u7a0b1\u7531\u4e8e\u4e0d\u77e5\u9053\u7ebf\u7a0b2\u5bf9stop\u53d8\u91cf\u7684\u66f4\u6539\uff0c\u56e0\u6b64\u8fd8\u4f1a\u4e00\u76f4\u5faa\u73af\u4e0b\u53bb\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4f46\u662f\u7528volatile\u4fee\u9970\u4e4b\u540e\u5c31\u53d8\u5f97\u4e0d\u4e00\u6837\u4e86\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e00\uff1a\u4f7f\u7528volatile\u5173\u952e\u5b57\u4f1a\u5f3a\u5236\u5c06\u4fee\u6539\u7684\u503c\u7acb\u5373\u5199\u5165\u4e3b\u5b58\uff1b<\/p>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e8c\uff1a\u4f7f\u7528volatile\u5173\u952e\u5b57\u7684\u8bdd\uff0c\u5f53\u7ebf\u7a0b2\u8fdb\u884c\u4fee\u6539\u65f6\uff0c\u4f1a\u5bfc\u81f4\u7ebf\u7a0b1\u7684\u5de5\u4f5c\u5185\u5b58\u4e2d\u7f13\u5b58\u53d8\u91cfstop\u7684\u7f13\u5b58\u884c\u65e0\u6548\uff08\u53cd\u6620\u5230\u786c\u4ef6\u5c42\u7684\u8bdd\uff0c\u5c31\u662fCPU\u7684L1\u6216\u8005L2\u7f13\u5b58\u4e2d\u5bf9\u5e94\u7684\u7f13\u5b58\u884c\u65e0\u6548\uff09\uff1b<\/p>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e09\uff1a\u7531\u4e8e\u7ebf\u7a0b1\u7684\u5de5\u4f5c\u5185\u5b58\u4e2d\u7f13\u5b58\u53d8\u91cfstop\u7684\u7f13\u5b58\u884c\u65e0\u6548\uff0c\u6240\u4ee5\u7ebf\u7a0b1\u518d\u6b21\u8bfb\u53d6\u53d8\u91cfstop\u7684\u503c\u65f6\u4f1a\u53bb\u4e3b\u5b58\u8bfb\u53d6\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\u5728\u7ebf\u7a0b2\u4fee\u6539stop\u503c\u65f6\uff08\u5f53\u7136\u8fd9\u91cc\u5305\u62ec2\u4e2a\u64cd\u4f5c\uff0c\u4fee\u6539\u7ebf\u7a0b2\u5de5\u4f5c\u5185\u5b58\u4e2d\u7684\u503c\uff0c\u7136\u540e\u5c06\u4fee\u6539\u540e\u7684\u503c\u5199\u5165\u5185\u5b58\uff09\uff0c\u4f1a\u4f7f\u5f97\u7ebf\u7a0b1\u7684\u5de5\u4f5c\u5185\u5b58\u4e2d\u7f13\u5b58\u53d8\u91cfstop\u7684\u7f13\u5b58\u884c\u65e0\u6548\uff0c\u7136\u540e\u7ebf\u7a0b1\u8bfb\u53d6\u65f6\uff0c\u53d1\u73b0\u81ea\u5df1\u7684\u7f13\u5b58\u884c\u65e0\u6548\uff0c\u5b83\u4f1a\u7b49\u5f85\u7f13\u5b58\u884c\u5bf9\u5e94\u7684\u4e3b\u5b58\u5730\u5740\u88ab\u66f4\u65b0\u4e4b\u540e\uff0c\u7136\u540e\u53bb\u5bf9\u5e94\u7684\u4e3b\u5b58\u8bfb\u53d6\u6700\u65b0\u7684\u503c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\u7ebf\u7a0b1\u8bfb\u53d6\u5230\u7684\u5c31\u662f\u6700\u65b0\u7684\u6b63\u786e\u7684\u503c\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2.volatile\u4e0d\u80fd\u786e\u4fdd\u539f\u5b50\u6027<\/h3>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Test {\n\u00a0\u00a0\u00a0\u00a0public volatile int inc = 0;\n\n\u00a0\u00a0\u00a0\u00a0public void increase() {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0inc++;\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0public static void main(String[] args) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0final Test test = new Test();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(int i=0;i&lt;10;i++){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0new Thread(){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run() {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(int j=0;j&lt;1000;j++)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0test.increase();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}.start();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while(Thread.activeCount()&gt;1)\u00a0 \/\/\u4fdd\u8bc1\u524d\u9762\u7684\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Thread.yield();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(test.inc);\n\u00a0\u00a0\u00a0\u00a0}\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5927\u5bb6\u60f3\u4e00\u4e0b\u8fd9\u6bb5\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u662f\u591a\u5c11\uff1f\u4e5f\u8bb8\u6709\u4e9b\u670b\u53cb\u8ba4\u4e3a\u662f10000\u3002\u4f46\u662f\u4e8b\u5b9e\u4e0a\u8fd0\u884c\u5b83\u4f1a\u53d1\u73b0\u6bcf\u6b21\u8fd0\u884c\u7ed3\u679c\u90fd\u4e0d\u4e00\u81f4\uff0c\u90fd\u662f\u4e00\u4e2a\u5c0f\u4e8e10000\u7684\u6570\u5b57\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u53ef\u80fd\u6709\u7684\u670b\u53cb\u5c31\u4f1a\u6709\u7591\u95ee\uff0c\u4e0d\u5bf9\u554a\uff0c\u4e0a\u9762\u662f\u5bf9\u53d8\u91cfinc\u8fdb\u884c\u81ea\u589e\u64cd\u4f5c\uff0c\u7531\u4e8evolatile\u4fdd\u8bc1\u4e86\u53ef\u89c1\u6027\uff0c\u90a3\u4e48\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u5bf9inc\u81ea\u589e\u5b8c\u4e4b\u540e\uff0c\u5728\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u90fd\u80fd\u770b\u5230\u4fee\u6539\u540e\u7684\u503c\u554a\uff0c\u6240\u4ee5\u670910\u4e2a\u7ebf\u7a0b\u5206\u522b\u8fdb\u884c\u4e861000\u6b21\u64cd\u4f5c\uff0c\u90a3\u4e48\u6700\u7ec8inc\u7684\u503c\u5e94\u8be5\u662f1000*10=10000\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u8fd9\u91cc\u9762\u5c31\u6709\u4e00\u4e2a\u8bef\u533a\u4e86\uff0cvolatile\u5173\u952e\u5b57\u80fd\u4fdd\u8bc1\u53ef\u89c1\u6027\u6ca1\u6709\u9519\uff0c\u4f46\u662f\u4e0a\u9762\u7684\u7a0b\u5e8f\u9519\u5728\u6ca1\u80fd\u4fdd\u8bc1\u539f\u5b50\u6027\u3002\u53ef\u89c1\u6027\u53ea\u80fd\u4fdd\u8bc1\u6bcf\u6b21\u8bfb\u53d6\u7684\u662f\u6700\u65b0\u7684\u503c\uff0c\u4f46\u662fvolatile\u6ca1\u529e\u6cd5\u4fdd\u8bc1\u5bf9\u53d8\u91cf\u7684\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5728\u524d\u9762\u5df2\u7ecf\u63d0\u5230\u8fc7\uff0c\u81ea\u589e\u64cd\u4f5c\u662f\u4e0d\u5177\u5907\u539f\u5b50\u6027\u7684\uff0c\u5b83\u5305\u62ec\u8bfb\u53d6\u53d8\u91cf\u7684\u539f\u59cb\u503c\u3001\u8fdb\u884c\u52a01\u64cd\u4f5c\u3001\u5199\u5165\u5de5\u4f5c\u5185\u5b58\u3002\u90a3\u4e48\u5c31\u662f\u8bf4\u81ea\u589e\u64cd\u4f5c\u7684\u4e09\u4e2a\u5b50\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5206\u5272\u5f00\u6267\u884c\uff0c\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u4e0b\u9762\u8fd9\u79cd\u60c5\u51b5\u51fa\u73b0\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">\u5047\u5982\u67d0\u4e2a\u65f6\u523b\u53d8\u91cfinc\u7684\u503c\u4e3a10\uff0c<\/p>\n<p style=\"margin: 1em 0px;\">\u7ebf\u7a0b1\u5bf9\u53d8\u91cf\u8fdb\u884c\u81ea\u589e\u64cd\u4f5c\uff0c\u7ebf\u7a0b1\u5148\u8bfb\u53d6\u4e86\u53d8\u91cfinc\u7684\u539f\u59cb\u503c\uff0c\u7136\u540e\u7ebf\u7a0b1\u88ab\u963b\u585e\u4e86\uff1b<\/p>\n<p style=\"margin: 1em 0px;\">\u7136\u540e\u7ebf\u7a0b2\u5bf9\u53d8\u91cf\u8fdb\u884c\u81ea\u589e\u64cd\u4f5c\uff0c\u7ebf\u7a0b2\u4e5f\u53bb\u8bfb\u53d6\u53d8\u91cfinc\u7684\u539f\u59cb\u503c\uff0c\u7531\u4e8e\u7ebf\u7a0b1\u53ea\u662f\u5bf9\u53d8\u91cfinc\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\uff0c\u800c\u6ca1\u6709\u5bf9\u53d8\u91cf\u8fdb\u884c\u4fee\u6539\u64cd\u4f5c\uff0c\u6240\u4ee5\u4e0d\u4f1a\u5bfc\u81f4\u7ebf\u7a0b2\u7684\u5de5\u4f5c\u5185\u5b58\u4e2d\u7f13\u5b58\u53d8\u91cfinc\u7684\u7f13\u5b58\u884c\u65e0\u6548\uff0c\u4e5f\u4e0d\u4f1a\u5bfc\u81f4\u4e3b\u5b58\u4e2d\u7684\u503c\u5237\u65b0\uff0c\u6240\u4ee5\u7ebf\u7a0b2\u4f1a\u76f4\u63a5\u53bb\u4e3b\u5b58\u8bfb\u53d6inc\u7684\u503c\uff0c\u53d1\u73b0inc\u7684\u503c\u65f610\uff0c\u7136\u540e\u8fdb\u884c\u52a01\u64cd\u4f5c\uff0c\u5e76\u628a11\u5199\u5165\u5de5\u4f5c\u5185\u5b58\uff0c\u6700\u540e\u5199\u5165\u4e3b\u5b58\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7136\u540e\u7ebf\u7a0b1\u63a5\u7740\u8fdb\u884c\u52a01\u64cd\u4f5c\uff0c\u7531\u4e8e\u5df2\u7ecf\u8bfb\u53d6\u4e86inc\u7684\u503c\uff0c\u6ce8\u610f\u6b64\u65f6\u5728\u7ebf\u7a0b1\u7684\u5de5\u4f5c\u5185\u5b58\u4e2dinc\u7684\u503c\u4ecd\u7136\u4e3a10\uff0c\u6240\u4ee5\u7ebf\u7a0b1\u5bf9inc\u8fdb\u884c\u52a01\u64cd\u4f5c\u540einc\u7684\u503c\u4e3a11\uff0c\u7136\u540e\u5c0611\u5199\u5165\u5de5\u4f5c\u5185\u5b58\uff0c\u6700\u540e\u5199\u5165\u4e3b\u5b58\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\u4e24\u4e2a\u7ebf\u7a0b\u5206\u522b\u8fdb\u884c\u4e86\u4e00\u6b21\u81ea\u589e\u64cd\u4f5c\u540e\uff0cinc\u53ea\u589e\u52a0\u4e861\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6839\u6e90\u5c31\u5728\u8fd9\u91cc\uff0c\u81ea\u589e\u64cd\u4f5c\u4e0d\u662f\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u800c\u4e14volatile\u4e5f\u65e0\u6cd5\u4fdd\u8bc1\u5bf9\u53d8\u91cf\u7684\u4efb\u4f55\u64cd\u4f5c\u90fd\u662f\u539f\u5b50\u6027\u7684\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u89e3\u51b3\u65b9\u6848\uff1a\u53ef\u4ee5\u901a\u8fc7synchronized\u6216lock\uff0c\u8fdb\u884c\u52a0\u9501\uff0c\u6765\u4fdd\u8bc1\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7AtomicInteger\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5728java 1.5\u7684java.util.concurrent.atomic\u5305\u4e0b\u63d0\u4f9b\u4e86\u4e00\u4e9b\u539f\u5b50\u64cd\u4f5c\u7c7b\uff0c\u5373\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684 \u81ea\u589e\uff08\u52a01\u64cd\u4f5c\uff09\uff0c\u81ea\u51cf\uff08\u51cf1\u64cd\u4f5c\uff09\u3001\u4ee5\u53ca\u52a0\u6cd5\u64cd\u4f5c\uff08\u52a0\u4e00\u4e2a\u6570\uff09\uff0c\u51cf\u6cd5\u64cd\u4f5c\uff08\u51cf\u4e00\u4e2a\u6570\uff09\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u4fdd\u8bc1\u8fd9\u4e9b\u64cd\u4f5c\u662f\u539f\u5b50\u6027\u64cd\u4f5c\u3002atomic\u662f\u5229\u7528CAS\u6765\u5b9e\u73b0\u539f\u5b50\u6027\u64cd\u4f5c\u7684\uff08Compare And Swap\uff09\uff0cCAS\u5b9e\u9645\u4e0a\u662f\u5229\u7528\u5904\u7406\u5668\u63d0\u4f9b\u7684CMPXCHG\u6307\u4ee4\u5b9e\u73b0\u7684\uff0c\u800c\u5904\u7406\u5668\u6267\u884cCMPXCHG\u6307\u4ee4\u662f\u4e00\u4e2a\u539f\u5b50\u6027\u64cd\u4f5c\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3.volatile\u4fdd\u8bc1\u6709\u5e8f\u6027<\/h3>\n<p style=\"margin: 1em 0px;\">\u5728\u524d\u9762\u63d0\u5230volatile\u5173\u952e\u5b57\u80fd\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u6240\u4ee5volatile\u80fd\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u4fdd\u8bc1\u6709\u5e8f\u6027\u3002<\/p>\n<p style=\"margin: 1em 0px;\">volatile\u5173\u952e\u5b57\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\u6709\u4e24\u5c42\u610f\u601d\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1\uff09\u5f53\u7a0b\u5e8f\u6267\u884c\u5230volatile\u53d8\u91cf\u7684\u8bfb\u64cd\u4f5c\u6216\u8005\u5199\u64cd\u4f5c\u65f6\uff0c\u5728\u5176\u524d\u9762\u7684\u64cd\u4f5c\u7684\u66f4\u6539\u80af\u5b9a\u5168\u90e8\u5df2\u7ecf\u8fdb\u884c\uff0c\u4e14\u7ed3\u679c\u5df2\u7ecf\u5bf9\u540e\u9762\u7684\u64cd\u4f5c\u53ef\u89c1\uff1b\u5728\u5176\u540e\u9762\u7684\u64cd\u4f5c\u80af\u5b9a\u8fd8\u6ca1\u6709\u8fdb\u884c\uff1b<\/p>\n<p style=\"margin: 1em 0px;\">2\uff09\u5728\u8fdb\u884c\u6307\u4ee4\u4f18\u5316\u65f6\uff0c\u4e0d\u80fd\u5c06\u5728\u5bf9volatile\u53d8\u91cf\u7684\u8bfb\u64cd\u4f5c\u6216\u8005\u5199\u64cd\u4f5c\u7684\u8bed\u53e5\u653e\u5728\u5176\u540e\u9762\u6267\u884c\uff0c\u4e5f\u4e0d\u80fd\u628avolatile\u53d8\u91cf\u540e\u9762\u7684\u8bed\u53e5\u653e\u5230\u5176\u524d\u9762\u6267\u884c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u53ef\u80fd\u4e0a\u9762\u8bf4\u7684\u6bd4\u8f83\u7ed5\uff0c\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/x\u3001y\u4e3a\u975evolatile\u53d8\u91cf\n\/\/flag\u4e3avolatile\u53d8\u91cf\n\nx = 2;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e51\ny = 0;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e52\nflag = true;\u00a0 \/\/\u8bed\u53e53\nx = 4;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e54\ny = -1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e55<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7531\u4e8eflag\u53d8\u91cf\u4e3avolatile\u53d8\u91cf\uff0c\u90a3\u4e48\u5728\u8fdb\u884c\u6307\u4ee4\u91cd\u6392\u5e8f\u7684\u8fc7\u7a0b\u7684\u65f6\u5019\uff0c\u4e0d\u4f1a\u5c06\u8bed\u53e53\u653e\u5230\u8bed\u53e51\u3001\u8bed\u53e52\u524d\u9762\uff0c\u4e5f\u4e0d\u4f1a\u8bb2\u8bed\u53e53\u653e\u5230\u8bed\u53e54\u3001\u8bed\u53e55\u540e\u9762\u3002\u4f46\u662f\u8981\u6ce8\u610f\u8bed\u53e51\u548c\u8bed\u53e52\u7684\u987a\u5e8f\u3001\u8bed\u53e54\u548c\u8bed\u53e55\u7684\u987a\u5e8f\u662f\u4e0d\u4f5c\u4efb\u4f55\u4fdd\u8bc1\u7684\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5e76\u4e14volatile\u5173\u952e\u5b57\u80fd\u4fdd\u8bc1\uff0c\u6267\u884c\u5230\u8bed\u53e53\u65f6\uff0c\u8bed\u53e51\u548c\u8bed\u53e52\u5fc5\u5b9a\u662f\u6267\u884c\u5b8c\u6bd5\u4e86\u7684\uff0c\u4e14\u8bed\u53e51\u548c\u8bed\u53e52\u7684\u6267\u884c\u7ed3\u679c\u5bf9\u8bed\u53e53\u3001\u8bed\u53e54\u3001\u8bed\u53e55\u662f\u53ef\u89c1\u7684\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\u6211\u4eec\u56de\u5230\u524d\u9762\u4e3e\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/\u7ebf\u7a0b1:\ncontext = loadContext();\u00a0\u00a0 \/\/\u8bed\u53e51\ninited = true;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8bed\u53e52\n\n\/\/\u7ebf\u7a0b2:\nwhile(!inited ){\n\u00a0\u00a0sleep()\n}\ndoSomethingwithconfig(context);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u524d\u9762\u4e3e\u8fd9\u4e2a\u4f8b\u5b50\u7684\u65f6\u5019\uff0c\u63d0\u5230\u6709\u53ef\u80fd\u8bed\u53e52\u4f1a\u5728\u8bed\u53e51\u4e4b\u524d\u6267\u884c\uff0c\u90a3\u4e48\u4e45\u53ef\u80fd\u5bfc\u81f4context\u8fd8\u6ca1\u88ab\u521d\u59cb\u5316\uff0c\u800c\u7ebf\u7a0b2\u4e2d\u5c31\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684context\u53bb\u8fdb\u884c\u64cd\u4f5c\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u9519\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u8fd9\u91cc\u5982\u679c\u7528volatile\u5173\u952e\u5b57\u5bf9inited\u53d8\u91cf\u8fdb\u884c\u4fee\u9970\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u79cd\u95ee\u9898\u4e86\uff0c\u56e0\u4e3a\u5f53\u6267\u884c\u5230\u8bed\u53e52\u65f6\uff0c\u5fc5\u5b9a\u80fd\u4fdd\u8bc1context\u5df2\u7ecf\u521d\u59cb\u5316\u5b8c\u6bd5\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u516d\u3001volatile\u7684\u5b9e\u73b0\u539f\u7406<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1.\u53ef\u89c1\u6027<\/h3>\n<p style=\"margin: 1em 0px;\">\u5904\u7406\u5668\u4e3a\u4e86\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\uff0c\u4e0d\u76f4\u63a5\u548c\u5185\u5b58\u8fdb\u884c\u901a\u8baf\uff0c\u800c\u662f\u5c06\u7cfb\u7edf\u5185\u5b58\u7684\u6570\u636e\u72ec\u5230\u5185\u90e8\u7f13\u5b58\u540e\u518d\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f46\u64cd\u4f5c\u5b8c\u540e\u4e0d\u77e5\u4ec0\u4e48\u65f6\u5019\u4f1a\u5199\u5230\u5185\u5b58\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5982\u679c\u5bf9\u58f0\u660e\u4e86volatile\u53d8\u91cf\u8fdb\u884c\u5199\u64cd\u4f5c\u65f6\uff0cJVM\u4f1a\u5411\u5904\u7406\u5668\u53d1\u9001\u4e00\u6761Lock\u524d\u7f00\u7684\u6307\u4ee4\uff0c\u5c06\u8fd9\u4e2a\u53d8\u91cf\u6240\u5728\u7f13\u5b58\u884c\u7684\u6570\u636e\u5199\u4f1a\u5230\u7cfb\u7edf\u5185\u5b58\u3002\u00a0\u8fd9\u4e00\u6b65\u786e\u4fdd\u4e86\u5982\u679c\u6709\u5176\u4ed6\u7ebf\u7a0b\u5bf9\u58f0\u660e\u4e86volatile\u53d8\u91cf\u8fdb\u884c\u4fee\u6539\uff0c\u5219\u7acb\u5373\u66f4\u65b0\u4e3b\u5185\u5b58\u4e2d\u6570\u636e\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4f46\u8fd9\u65f6\u5019\u5176\u4ed6\u5904\u7406\u5668\u7684\u7f13\u5b58\u8fd8\u662f\u65e7\u7684\uff0c\u6240\u4ee5\u5728\u591a\u5904\u7406\u5668\u73af\u5883\u4e0b\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u5404\u4e2a\u5904\u7406\u5668\u7f13\u5b58\u4e00\u81f4\uff0c\u6bcf\u4e2a\u5904\u7406\u4f1a\u901a\u8fc7\u55c5\u63a2\u5728\u603b\u7ebf\u4e0a\u4f20\u64ad\u7684\u6570\u636e\u6765\u68c0\u67e5 \u81ea\u5df1\u7684\u7f13\u5b58\u662f\u5426\u8fc7\u671f\uff0c\u5f53\u5904\u7406\u5668\u53d1\u73b0\u81ea\u5df1\u7f13\u5b58\u884c\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u88ab\u4fee\u6539\u4e86\uff0c\u5c31\u4f1a\u5c06\u5f53\u524d\u5904\u7406\u5668\u7684\u7f13\u5b58\u884c\u8bbe\u7f6e\u6210\u65e0\u6548\u72b6\u6001\uff0c\u5f53\u5904\u7406\u5668\u8981\u5bf9\u8fd9\u4e2a\u6570\u636e\u8fdb\u884c\u4fee\u6539\u64cd\u4f5c\u65f6\uff0c\u4f1a\u5f3a\u5236\u91cd\u65b0\u4ece\u7cfb\u7edf\u5185\u5b58\u628a\u6570\u636e\u8bfb\u5230\u5904\u7406\u5668\u7f13\u5b58\u91cc\u3002\u00a0\u8fd9\u4e00\u6b65\u786e\u4fdd\u4e86\u5176\u4ed6\u7ebf\u7a0b\u83b7\u5f97\u7684\u58f0\u660e\u4e86volatile\u53d8\u91cf\u90fd\u662f\u4ece\u4e3b\u5185\u5b58\u4e2d\u83b7\u53d6\u6700\u65b0\u7684\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2.\u6709\u5e8f\u6027<\/h3>\n<p style=\"margin: 1em 0px;\">Lock\u524d\u7f00\u6307\u4ee4\u5b9e\u9645\u4e0a\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5185\u5b58\u5c4f\u969c\uff08\u4e5f\u6210\u5185\u5b58\u6805\u680f\uff09\uff0c\u5b83\u786e\u4fdd\u6307\u4ee4\u91cd\u6392\u5e8f\u65f6\u4e0d\u4f1a\u628a\u5176\u540e\u9762\u7684\u6307\u4ee4\u6392\u5230\u5185\u5b58\u5c4f\u969c\u4e4b\u524d\u7684\u4f4d\u7f6e\uff0c\u4e5f\u4e0d\u4f1a\u628a\u524d\u9762\u7684\u6307\u4ee4\u6392\u5230\u5185\u5b58\u5c4f\u969c\u7684\u540e\u9762\uff1b\u5373\u5728\u6267\u884c\u5230\u5185\u5b58\u5c4f\u969c\u8fd9\u53e5\u6307\u4ee4\u65f6\uff0c\u5728\u5b83\u524d\u9762\u7684\u64cd\u4f5c\u5df2\u7ecf\u5168\u90e8\u5b8c\u6210\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e03\u3001volatile\u7684\u5e94\u7528\u573a\u666f<\/h2>\n<p style=\"margin: 1em 0px;\">synchronized\u5173\u952e\u5b57\u662f\u9632\u6b62\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\uff0c\u90a3\u4e48\u5c31\u4f1a\u5f88\u5f71\u54cd\u7a0b\u5e8f\u6267\u884c\u6548\u7387\uff0c\u800cvolatile\u5173\u952e\u5b57\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6027\u80fd\u8981\u4f18\u4e8esynchronized\uff0c\u4f46\u662f\u8981\u6ce8\u610fvolatile\u5173\u952e\u5b57\u662f\u65e0\u6cd5\u66ff\u4ee3synchronized\u5173\u952e\u5b57\u7684\uff0c\u56e0\u4e3avolatile\u5173\u952e\u5b57\u65e0\u6cd5\u4fdd\u8bc1\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002\u901a\u5e38\u6765\u8bf4\uff0c\u4f7f\u7528volatile\u5fc5\u987b\u5177\u5907\u4ee5\u4e0b2\u4e2a\u6761\u4ef6\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1\uff09\u5bf9\u53d8\u91cf\u7684\u5199\u64cd\u4f5c\u4e0d\u4f9d\u8d56\u4e8e\u5f53\u524d\u503c<\/p>\n<p style=\"margin: 1em 0px;\">2\uff09\u8be5\u53d8\u91cf\u6ca1\u6709\u5305\u542b\u5728\u5177\u6709\u5176\u4ed6\u53d8\u91cf\u7684\u4e0d\u53d8\u5f0f\u4e2d<\/p>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u5217\u4e3e\u51e0\u4e2aJava\u4e2d\u4f7f\u7528volatile\u7684\u51e0\u4e2a\u573a\u666f\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u2460.\u72b6\u6001\u6807\u8bb0\u91cf<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">volatile boolean flag = false;\n\u00a0\/\/\u7ebf\u7a0b1\nwhile(!flag){\n\u00a0\u00a0\u00a0\u00a0doSomething();\n}\n\u00a0\u00a0\/\/\u7ebf\u7a0b2\npublic void setFlag() {\n\u00a0\u00a0\u00a0\u00a0flag = true;\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6839\u636e\u72b6\u6001\u6807\u8bb0\uff0c\u7ec8\u6b62\u7ebf\u7a0b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u2461.\u5355\u4f8b\u6a21\u5f0f\u4e2d\u7684double check<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">class Singleton{\n\u00a0\u00a0\u00a0\u00a0private volatile static Singleton instance = null;\n\n\u00a0\u00a0\u00a0\u00a0private Singleton() {\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0public static Singleton getInstance() {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(instance==null) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (Singleton.class) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(instance==null)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0instance = new Singleton();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return instance;\n\u00a0\u00a0\u00a0\u00a0}\n}<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528volatile \u4fee\u9970instance\uff1f<\/h3>\n<p style=\"margin: 1em 0px;\">\u4e3b\u8981\u5728\u4e8einstance = new Singleton()\u8fd9\u53e5\uff0c\u8fd9\u5e76\u975e\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u4e8b\u5b9e\u4e0a\u5728 JVM \u4e2d\u8fd9\u53e5\u8bdd\u5927\u6982\u505a\u4e86\u4e0b\u9762 3 \u4ef6\u4e8b\u60c5:<\/p>\n<p style=\"margin: 1em 0px;\">1.\u7ed9 instance \u5206\u914d\u5185\u5b58<\/p>\n<p style=\"margin: 1em 0px;\">2.\u8c03\u7528 Singleton \u7684\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf<\/p>\n<p style=\"margin: 1em 0px;\">3.\u5c06instance\u5bf9\u8c61\u6307\u5411\u5206\u914d\u7684\u5185\u5b58\u7a7a\u95f4\uff08\u6267\u884c\u5b8c\u8fd9\u6b65 instance \u5c31\u4e3a\u975e null \u4e86\uff09\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4f46\u662f\u5728 JVM \u7684\u5373\u65f6\u7f16\u8bd1\u5668\u4e2d\u5b58\u5728\u6307\u4ee4\u91cd\u6392\u5e8f\u7684\u4f18\u5316\u3002\u4e5f\u5c31\u662f\u8bf4\u4e0a\u9762\u7684\u7b2c\u4e8c\u6b65\u548c\u7b2c\u4e09\u6b65\u7684\u987a\u5e8f\u662f\u4e0d\u80fd\u4fdd\u8bc1\u7684\uff0c\u6700\u7ec8\u7684\u6267\u884c\u987a\u5e8f\u53ef\u80fd\u662f 1-2-3 \u4e5f\u53ef\u80fd\u662f 1-3-2\u3002\u5982\u679c\u662f\u540e\u8005\uff0c\u5219\u5728 3 \u6267\u884c\u5b8c\u6bd5\u30012 \u672a\u6267\u884c\u4e4b\u524d\uff0c\u88ab\u7ebf\u7a0b\u4e8c\u62a2\u5360\u4e86\uff0c\u8fd9\u65f6 instance \u5df2\u7ecf\u662f\u975e null \u4e86\uff08\u4f46\u5374\u6ca1\u6709\u521d\u59cb\u5316\uff09\uff0c\u6240\u4ee5\u7ebf\u7a0b\u4e8c\u4f1a\u76f4\u63a5\u8fd4\u56de instance\uff0c\u7136\u540e\u4f7f\u7528\uff0c\u7136\u540e\u987a\u7406\u6210\u7ae0\u5730\u62a5\u9519\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u539f\u6587\u51fa\u5904\uff1a <a href=\"http:\/\/www.jianshu.com\/p\/7798161d7472\" target=\"_blank\" rel=\"noopener\">Ruheng<\/a><\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1avolatile \u4fdd\u8bc1\u53ef\u89c1\u6027\u548c\u7981\u6b62\u6307\u4ee4\u91cd\u6392\uff0c\u4e0d\u4fdd\u8bc1\u590d\u5408\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff1b\u8ba1\u6570\u4ecd\u8981\u7528\u9501\u6216\u539f\u5b50\u7c7b\uff0c\u5355\u4f8b\u53cc\u91cd\u68c0\u67e5\u91cc\u7684 instance \u624d\u662f\u5b83\u7684\u5178\u578b\u7528\u6cd5\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u7406\u89e3 volatile \u4e3a\u4ec0\u4e48\u80fd\u4fdd\u8bc1\u53ef\u89c1\u6027\uff0c\u5148\u770b Java \u5185\u5b58\u6a21\u578b\uff1a\u53d8\u91cf\u5728\u4e3b\u5185\u5b58\uff0c\u7ebf\u7a0b\u5728\u5de5\u4f5c\u5185\u5b58\u91cc\u64cd\u4f5c\u62f7\u8d1d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3498,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[33,30],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2109"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2109"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2109\/revisions"}],"predecessor-version":[{"id":3718,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2109\/revisions\/3718"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/media\/3498"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}