{"id":93095,"date":"2026-08-11T22:48:46","date_gmt":"2026-08-11T14:48:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/93095.html"},"modified":"2026-08-11T22:48:46","modified_gmt":"2026-08-11T14:48:46","slug":"%e3%80%90jvm%e5%8e%9f%e7%90%86%e8%af%a6%e8%a7%a3%e3%80%9146-%e9%94%81%e7%9a%84%e5%86%85%e5%ad%98%e8%af%ad%e4%b9%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/93095.html","title":{"rendered":"\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49"},"content":{"rendered":"<h2>46-\u9501\u7684\u5185\u5b58\u8bed\u4e49<\/h2>\n<h3>\u5f15\u8a00<\/h3>\n<p>\u524d\u51e0\u7bc7\u6211\u4eec\u5206\u522b\u8bb2\u4e86volatile\u548cfinal\u7684\u5185\u5b58\u8bed\u4e49\u3002volatile\u7528\u5185\u5b58\u5c4f\u969c\u4fdd\u8bc1\u53ef\u89c1\u6027\u548c\u6709\u5e8f\u6027&#xff0c;final\u7528StoreStore\/LoadLoad\u5c4f\u969c\u4fdd\u8bc1\u521d\u59cb\u5316\u5b89\u5168\u3002\u4f46Java\u5e76\u53d1\u4e2d\u8fd8\u6709\u4e00\u7c7b\u6700\u5e38\u7528\u7684\u540c\u6b65\u624b\u6bb5\u2014\u2014\u9501\u3002<\/p>\n<p>\u9501\u4e0d\u53ea\u662f&#034;\u4e92\u65a5&#034;\u5de5\u5177&#xff0c;\u5b83\u540c\u6837\u627f\u62c5\u5185\u5b58\u540c\u6b65\u804c\u8d23\u3002\u65e0\u8bba\u662f\u5185\u7f6e\u7684synchronized&#xff0c;\u8fd8\u662fjava.util.concurrent.locks.ReentrantLock&#xff0c;\u751a\u81f3\u662f\u57fa\u4e8eCAS\u7684\u539f\u5b50\u7c7b&#xff0c;\u5b83\u4eec\u90fd\u6709\u660e\u786e\u7684\u5185\u5b58\u8bed\u4e49&#xff1a;\u52a0\u9501\u65f6\u5237\u65b0\u5de5\u4f5c\u5185\u5b58&#xff0c;\u89e3\u9501\u65f6\u5199\u56de\u4e3b\u5185\u5b58\u3002\u7406\u89e3\u9501\u7684\u5185\u5b58\u8bed\u4e49&#xff0c;\u624d\u80fd\u660e\u767d\u4e3a\u4ec0\u4e48&#034;synchronized\u5757\u5185\u7684\u5199\u5bf9\u4e0b\u4e00\u4e2a\u83b7\u53d6\u9501\u7684\u7ebf\u7a0b\u53ef\u89c1&#034;&#xff0c;\u4ee5\u53ca\u4e3a\u4ec0\u4e48ReentrantLock\u80fd\u66ff\u4ee3synchronized\u3002<\/p>\n<p>\u672c\u7bc7\u4ecesynchronized\u7684\u5185\u5b58\u8bed\u4e49\u5207\u5165&#xff0c;\u6269\u5c55\u5230ReentrantLock&#xff08;AQS\u7684volatile state&#xff09;\u3001CAS\u7684\u5185\u5b58\u8bed\u4e49&#xff0c;\u6700\u540e\u7528happens-before\u5173\u7cfb\u5bf9\u6bd4\u4e09\u79cd\u540c\u6b65\u65b9\u5f0f\u3002<\/p>\n<h3>synchronized\u7684\u5185\u5b58\u8bed\u4e49<\/h3>\n<p>synchronized\u662fJava\u6700\u57fa\u7840\u7684\u9501\u673a\u5236&#xff0c;\u5e95\u5c42\u7531JVM\u7684monitorenter\/monitorexit\u6307\u4ee4\u548c\u5bf9\u8c61\u5934\u7684Mark Word\u5b9e\u73b0\u3002\u4f46\u5b83\u9664\u4e86\u4e92\u65a5&#xff0c;\u8fd8\u6709\u660e\u786e\u7684\u5185\u5b58\u540c\u6b65\u8bed\u4e49\u3002<\/p>\n<h4>\u52a0\u9501\u4e0e\u89e3\u9501\u7684\u8bed\u4e49<\/h4>\n<p>JMM\u5bf9synchronized\u7684\u5185\u5b58\u8bed\u4e49\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u52a0\u9501&#xff08;monitorenter \/ \u8fdb\u5165synchronized\u5757&#xff09;&#xff1a;\u6e05\u7a7a\u5f53\u524d\u7ebf\u7a0b\u7684\u5de5\u4f5c\u5185\u5b58&#xff08;\u672c\u5730\u7f13\u5b58\u526f\u672c&#xff09;&#xff0c;\u76f8\u5f53\u4e8e\u5f3a\u5236\u540e\u7eed\u8bfb\u53d6\u4ece\u4e3b\u5185\u5b58\u91cd\u65b0\u52a0\u8f7d<\/li>\n<li>\u89e3\u9501&#xff08;monitorexit \/ \u9000\u51fasynchronized\u5757&#xff09;&#xff1a;\u628a\u5de5\u4f5c\u5185\u5b58\u4e2d\u6240\u6709\u4fee\u6539\u8fc7\u7684\u503c\u5199\u56de\u4e3b\u5185\u5b58&#xff0c;\u76f8\u5f53\u4e8e\u6267\u884cStoreStore &#043; StoreLoad\u5c4f\u969c<\/li>\n<\/ul>\n<p>\u66f4\u76f4\u89c2\u7684\u7406\u89e3&#xff1a;<\/p>\n<p>\u7ebf\u7a0bA&#xff08;\u6301\u9501\u5e76\u4fee\u6539\u6570\u636e&#xff09;:<br \/>\n   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n   \u2502 synchronized(lock) {     \u2502  \u2190 \u52a0\u9501&#xff1a;\u6e05\u7a7a\u5de5\u4f5c\u5185\u5b58&#xff0c;\u540e\u7eed\u8bfb\u4ece\u4e3b\u5185\u5b58\u53d6<br \/>\n   \u2502   read sharedData        \u2502     &#xff08;\u83b7\u53d6\u9501\u65f6\u7684\u6700\u65b0\u503c&#xff09;<br \/>\n   \u2502   sharedData &#061; newValue  \u2502     &#xff08;\u5728\u5de5\u4f5c\u5185\u5b58\u4fee\u6539&#xff09;<br \/>\n   \u2502 }                        \u2502  \u2190 \u89e3\u9501&#xff1a;\u5199\u56de\u4e3b\u5185\u5b58&#xff08;StoreStore &#043; StoreLoad&#xff09;<br \/>\n   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>\u7ebf\u7a0bB&#xff08;\u968f\u540e\u83b7\u53d6\u540c\u4e00\u628a\u9501&#xff09;:<br \/>\n   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n   \u2502 synchronized(lock) {     \u2502  \u2190 \u52a0\u9501&#xff1a;\u6e05\u7a7a\u5de5\u4f5c\u5185\u5b58<br \/>\n   \u2502   read sharedData        \u2502     &#xff08;\u4ece\u4e3b\u5185\u5b58\u8bfb\u5230\u7ebf\u7a0bA\u5199\u5165\u7684newValue&#xff09;<br \/>\n   \u2502 }                        \u2502<br \/>\n   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>\u8fd9\u548cvolatile\u7684&#034;\u5199\u5237\u65b0\u4e3b\u5185\u5b58\u3001\u8bfb\u5f3a\u5236\u91cd\u8f7d&#034;\u673a\u5236\u672c\u8d28\u76f8\u540c&#xff0c;\u53ea\u662f\u4f5c\u7528\u8303\u56f4\u66f4\u5927\u2014\u2014volatile\u4f5c\u7528\u4e8e\u5355\u4e2a\u5b57\u6bb5&#xff0c;synchronized\u4f5c\u7528\u4e8e\u6574\u4e2a\u4e34\u754c\u533a\u5185\u7684\u6240\u6709\u5171\u4eab\u53d8\u91cf\u3002<\/p>\n<h4>\u4e3a\u4ec0\u4e48\u662f&#034;\u6e05\u7a7a\u5de5\u4f5c\u5185\u5b58&#034;<\/h4>\n<p>synchronized\u52a0\u9501\u65f6&#034;\u6e05\u7a7a\u5de5\u4f5c\u5185\u5b58&#034;\u7684\u8bbe\u8ba1&#xff0c;\u76ee\u7684\u662f\u4fdd\u8bc1\u4e34\u754c\u533a\u5185\u7684\u8bfb\u53d6\u62ff\u5230\u6700\u65b0\u503c\u3002\u5982\u679c\u4e0d\u6e05\u7a7a&#xff0c;\u7ebf\u7a0b\u53ef\u80fd\u7528\u5de5\u4f5c\u5185\u5b58\u4e2d\u7684\u65e7\u526f\u672c&#xff0c;\u5bfc\u81f4\u8bfb\u5230\u8fc7\u671f\u6570\u636e\u3002<\/p>\n<p>\u5177\u4f53\u6765\u8bf4&#xff0c;JMM\u89c4\u5b9a&#xff1a;<\/p>\n<li>\u7ebf\u7a0b\u8fdb\u5165synchronized\u5757\u524d&#xff0c;\u5fc5\u987b\u628a\u6240\u6709\u4f7f\u7528\u7684\u53d8\u91cf\u4ece\u5de5\u4f5c\u5185\u5b58\u4e2d&#034;\u4e22\u5f03&#034;&#xff08;\u6807\u8bb0\u4e3a\u65e0\u6548&#xff09;<\/li>\n<li>\u4e34\u754c\u533a\u5185\u6bcf\u6b21\u8bfb\u53d6\u53d8\u91cf&#xff0c;\u90fd\u5fc5\u987b\u91cd\u65b0\u4ece\u4e3b\u5185\u5b58load&#xff08;\u56e0\u4e3a\u5de5\u4f5c\u5185\u5b58\u526f\u672c\u5df2\u5931\u6548&#xff09;<\/li>\n<li>\u9000\u51fasynchronized\u5757\u65f6&#xff0c;\u628a\u5de5\u4f5c\u5185\u5b58\u4e2d\u6240\u6709\u4fee\u6539\u8fc7\u7684\u503c\u540c\u6b65\u56de\u4e3b\u5185\u5b58<\/li>\n<p>\u8fd9\u6837&#xff0c;synchronized\u5757\u5c31\u6210\u4e86\u4e00\u4e2a**\u201c\u5185\u5b58\u5c4f\u969c\u5757\u201d**\u2014\u2014\u8fdb\u5165\u65f6\u5f3a\u5236\u4e0e\u4e3b\u5185\u5b58\u540c\u6b65&#xff0c;\u9000\u51fa\u65f6\u5f3a\u5236\u628a\u4fee\u6539\u540c\u6b65\u56de\u53bb\u3002<\/p>\n<h4>\u4ee3\u7801\u793a\u4f8b<\/h4>\n<p><span class=\"token comment\">\/\/ \u9002\u7528 JDK 8\/11\/17<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">SynchronizedMemoryDemo<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">int<\/span> data <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">boolean<\/span> flag <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token class-name\">Object<\/span> lock <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Object<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7ebf\u7a0bA&#xff1a;\u52a0\u9501\u4fee\u6539<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">writer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">synchronized<\/span> <span class=\"token punctuation\">(<\/span>lock<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>       <span class=\"token comment\">\/\/ \u52a0\u9501&#xff1a;\u6e05\u7a7a\u5de5\u4f5c\u5185\u5b58<\/span><br \/>\n            data <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>              <span class=\"token comment\">\/\/ \u4fee\u65391<\/span><br \/>\n            flag <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span>            <span class=\"token comment\">\/\/ \u4fee\u65392<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span>                            <span class=\"token comment\">\/\/ \u89e3\u9501&#xff1a;\u5199\u56de\u4e3b\u5185\u5b58&#xff08;data&#061;42, flag&#061;true&#xff09;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7ebf\u7a0bB&#xff1a;\u52a0\u9501\u8bfb\u53d6<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">reader<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">synchronized<\/span> <span class=\"token punctuation\">(<\/span>lock<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>       <span class=\"token comment\">\/\/ \u52a0\u9501&#xff1a;\u6e05\u7a7a\u5de5\u4f5c\u5185\u5b58&#xff0c;\u91cd\u65b0\u4ece\u4e3b\u5185\u5b58\u8bfb<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u56e0\u4e3aA\u7684\u89e3\u9501 hb B\u7684\u52a0\u9501&#xff08;\u76d1\u89c6\u5668\u9501\u89c4\u5219&#xff09;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u4e14A\u89e3\u9501\u65f6\u628adata\u548cflag\u5199\u56de\u4e86\u4e3b\u5185\u5b58<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u6240\u4ee5\u8fd9\u91cc\u5fc5\u7136\u8bfb\u5230 data&#061;42, flag&#061;true<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;data&#061;&#034;<\/span> <span class=\"token operator\">&#043;<\/span> data <span class=\"token operator\">&#043;<\/span> <span class=\"token string\">&#034;, flag&#061;&#034;<\/span> <span class=\"token operator\">&#043;<\/span> flag<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6ce8\u610f&#xff1a;\u5373\u4f7fdata\u548cflag\u4e0d\u662fvolatile&#xff0c;\u53ea\u8981\u5b83\u4eec\u5728synchronized\u5757\u5185\u88ab\u8bfb\u5199&#xff0c;\u4e14\u6240\u6709\u8bbf\u95ee\u90fd\u901a\u8fc7\u540c\u4e00\u628a\u9501&#xff0c;\u53ef\u89c1\u6027\u5c31\u6709\u4fdd\u8bc1\u3002\u4f46\u5982\u679c\u67d0\u7ebf\u7a0b\u5728\u4e0d\u52a0\u9501\u7684\u60c5\u51b5\u4e0b\u8bfbdata&#xff0c;\u5c31\u4e0d\u4fdd\u8bc1\u53ef\u89c1\u6027\u2014\u2014synchronized\u7684\u5185\u5b58\u8bed\u4e49\u53ea\u8986\u76d6&#034;\u540c\u4e00\u628a\u9501&#034;\u7684\u52a0\u9501-\u89e3\u9501\u5e8f\u5217\u3002<\/p>\n<h4>synchronized\u7684happens-before\u5173\u7cfb<\/h4>\n<p>\u7ed3\u5408\u4e0a\u4e00\u7bc7\u7684happens-before\u89c4\u5219&#xff0c;synchronized\u7684\u5185\u5b58\u8bed\u4e49\u53ef\u4ee5\u7528\u4e00\u6761\u89c4\u5219\u6982\u62ec&#xff1a;\u76d1\u89c6\u5668\u9501\u89c4\u5219\u2014\u2014\u5bf9\u540c\u4e00\u628a\u9501\u7684unlock\u64cd\u4f5chappens-before\u540e\u7eed\u7684lock\u64cd\u4f5c\u3002<\/p>\n<p>\u63a8\u6f14&#xff1a;<\/p>\n<p>\u7ebf\u7a0bA:  [\u4e34\u754c\u533a\u5185\u5199] \u2192 unlock<br \/>\n                         \u2502<br \/>\n                         \u2502 happens-before&#xff08;\u76d1\u89c6\u5668\u9501\u89c4\u5219&#xff09;<br \/>\n                         \u2193<br \/>\n\u7ebf\u7a0bB:               lock \u2192 [\u4e34\u754c\u533a\u5185\u8bfb]<\/p>\n<p>\u4f20\u9012\u6027\u6269\u5c55&#xff1a;\u7ebf\u7a0bA\u4e34\u754c\u533a\u5185\u7684\u6240\u6709\u5199\u64cd\u4f5c&#xff08;\u7a0b\u5e8f\u987a\u5e8f\u89c4\u5219&#xff1a;\u5199 hb unlock&#xff09;&#xff0c;\u901a\u8fc7\u76d1\u89c6\u5668\u9501\u89c4\u5219&#xff08;unlock hb lock&#xff09;&#xff0c;\u4f20\u9012\u5230\u7ebf\u7a0bB\u4e34\u754c\u533a\u5185\u7684\u6240\u6709\u8bfb\u64cd\u4f5c&#xff08;\u7a0b\u5e8f\u987a\u5e8f\u89c4\u5219&#xff1a;lock hb \u8bfb&#xff09;\u3002\u6240\u4ee5\u7ebf\u7a0bA\u4e34\u754c\u533a\u5185\u7684\u6240\u6709\u5199&#xff0c;\u5bf9\u7ebf\u7a0bB\u4e34\u754c\u533a\u5185\u7684\u6240\u6709\u8bfb\u53ef\u89c1\u3002<\/p>\n<h3>ReentrantLock\u7684\u5185\u5b58\u8bed\u4e49<\/h3>\n<p>ReentrantLock\u662fjava.util.concurrent.locks\u5305\u63d0\u4f9b\u7684\u663e\u5f0f\u9501&#xff0c;\u529f\u80fd\u6bd4synchronized\u66f4\u7075\u6d3b&#xff08;\u53ef\u4e2d\u65ad\u3001\u53ef\u8d85\u65f6\u3001\u516c\u5e73\/\u975e\u516c\u5e73&#xff09;\u3002\u5b83\u5728\u5185\u5b58\u8bed\u4e49\u4e0a\u4e0esynchronized\u7b49\u4ef7\u2014\u2014\u52a0\u9501\u548c unlocks\u5177\u6709\u76f8\u540c\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1\u3002\u5b9e\u73b0\u673a\u5236\u5219\u5b8c\u5168\u4e0d\u540c\u3002<\/p>\n<h4>AQS\u4e0evolatile state<\/h4>\n<p>ReentrantLock\u57fa\u4e8e**AQS&#xff08;AbstractQueuedSynchronizer&#xff09;**\u5b9e\u73b0\u3002AQS\u7684\u6838\u5fc3\u662f\u4e00\u4e2avolatile int state\u5b57\u6bb5&#xff0c;\u8868\u793a\u540c\u6b65\u72b6\u6001&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ AQS\u6838\u5fc3\u5b57\u6bb5&#xff08;\u7b80\u5316\u5c55\u793a&#xff09;<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">abstract<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">AbstractQueuedSynchronizer<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">volatile<\/span> <span class=\"token keyword\">int<\/span> state<span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ volatile&#xff01;<\/span><\/p>\n<p>    <span class=\"token keyword\">protected<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">getState<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token keyword\">return<\/span> state<span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">protected<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">setState<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> newState<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> state <span class=\"token operator\">&#061;<\/span> newState<span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">protected<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token keyword\">boolean<\/span> <span class=\"token function\">compareAndSetState<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> expect<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> update<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> unsafe<span class=\"token punctuation\">.<\/span><span class=\"token function\">compareAndSwapInt<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">,<\/span> stateOffset<span class=\"token punctuation\">,<\/span> expect<span class=\"token punctuation\">,<\/span> update<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>state\u662fvolatile&#xff0c;\u8fd9\u662fReentrantLock\u5185\u5b58\u8bed\u4e49\u7684\u951a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u52a0\u9501&#xff1a;\u901a\u8fc7CAS\u628astate\u4ece0\u6539\u4e3a1&#xff08;\u975e\u516c\u5e73&#xff09;\u6216\u6392\u961f\u540eCAS&#xff08;\u516c\u5e73&#xff09;\u3002CAS\u6210\u529f\u5373\u83b7\u53d6\u9501<\/li>\n<li>\u89e3\u9501&#xff1a;\u628astate\u4ece1\u6539\u4e3a0&#xff08;\u6216\u9012\u51cf\u91cd\u5165\u8ba1\u6570&#xff09;<\/li>\n<\/ul>\n<p>\u56e0\u4e3astate\u662fvolatile&#xff0c;\u52a0\u9501\u65f6\u7684CAS&#xff08;volatile\u5199&#xff09;\u548c\u89e3\u9501\u65f6\u7684setState&#xff08;volatile\u5199&#xff09;\u90fd\u9075\u5faavolatile\u7684\u5185\u5b58\u8bed\u4e49\u3002\u914d\u5408happens-before\u89c4\u5219&#xff0c;\u8fd9\u5c31\u5efa\u7acb\u4e86\u4e0esynchronized\u7b49\u4ef7\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1\u3002<\/p>\n<h4>lock\/unlock\u7684happens-before\u94fe<\/h4>\n<p>ReentrantLock\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1&#xff0c;\u53ef\u4ee5\u901a\u8fc7happens-before\u89c4\u5219\u63a8\u5bfc&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u9002\u7528 JDK 8\/11\/17<\/span><br \/>\n<span class=\"token keyword\">import<\/span> <span class=\"token import\"><span class=\"token namespace\">java<span class=\"token punctuation\">.<\/span>util<span class=\"token punctuation\">.<\/span>concurrent<span class=\"token punctuation\">.<\/span>locks<span class=\"token punctuation\">.<\/span><\/span><span class=\"token class-name\">ReentrantLock<\/span><\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ReentrantLockMemoryDemo<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">int<\/span> data <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token class-name\">ReentrantLock<\/span> lock <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ReentrantLock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7ebf\u7a0bA&#xff1a;\u52a0\u9501\u4fee\u6539<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">writer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>           <span class=\"token comment\">\/\/ \u64cd\u4f5c1&#xff1a;CAS state 0\u21921&#xff08;volatile\u5199&#xff09;<\/span><br \/>\n        <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            data <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>         <span class=\"token comment\">\/\/ \u64cd\u4f5c2&#xff1a;\u666e\u901a\u5199<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">finally<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>     <span class=\"token comment\">\/\/ \u64cd\u4f5c3&#xff1a;setState(0)&#xff08;volatile\u5199&#xff09;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7ebf\u7a0bB&#xff1a;\u52a0\u9501\u8bfb\u53d6<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">reader<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>           <span class=\"token comment\">\/\/ \u64cd\u4f5c4&#xff1a;CAS state&#xff08;volatile\u8bfb&#043;\u5199&#xff09;<\/span><br \/>\n        <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u63a8\u7406&#xff1a;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u7a0b\u5e8f\u987a\u5e8f&#xff1a;\u64cd\u4f5c2 hb \u64cd\u4f5c3&#xff08;\u540c\u4e00\u7ebf\u7a0b\u5185&#xff09;<\/span><br \/>\n            <span class=\"token comment\">\/\/ volatile\u89c4\u5219&#xff1a;\u64cd\u4f5c3(\u5199state) hb \u64cd\u4f5c4(\u8bfbstate)<\/span><br \/>\n            <span class=\"token comment\">\/\/   \u2014\u2014 \u56e0\u4e3aunlock\u5199state&#xff0c;lock\u8bfbstate&#xff0c;state\u662fvolatile<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u7a0b\u5e8f\u987a\u5e8f&#xff1a;\u64cd\u4f5c4 hb \u8bfbdata<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u4f20\u9012\u6027&#xff1a;\u64cd\u4f5c2 hb \u8bfbdata \u2192 data&#061;42\u5fc5\u7136\u53ef\u89c1<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>data<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">finally<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u5173\u952e\u94fe\u8def&#xff1a;<\/p>\n<li>\u7ebf\u7a0bA\u7684data&#061;42&#xff08;\u666e\u901a\u5199&#xff09;\u901a\u8fc7\u7a0b\u5e8f\u987a\u5e8f\u89c4\u5219&#xff0c;happens-before unlock()&#xff08;volatile\u5199state&#xff09;<\/li>\n<li>\u7ebf\u7a0bA\u7684unlock()&#xff08;\u5199volatile state&#xff09;\u901a\u8fc7volatile\u89c4\u5219&#xff0c;happens-before \u7ebf\u7a0bB\u7684lock()&#xff08;\u8bfbvolatile state&#xff09;<\/li>\n<li>\u7ebf\u7a0bB\u7684lock()\u901a\u8fc7\u7a0b\u5e8f\u987a\u5e8f\u89c4\u5219&#xff0c;happens-before \u8bfbdata<\/li>\n<li>\u4f20\u9012\u6027&#xff1a;data&#061;42 happens-before \u8bfbdata<\/li>\n<p>\u6240\u4ee5\u7ebf\u7a0bB\u5fc5\u7136\u8bfb\u5230data&#061;42\u3002\u8fd9\u6761\u63a8\u7406\u94fe\u548csynchronized\u5b8c\u5168\u5bf9\u79f0\u2014\u2014\u53ea\u662fsynchronized\u9760\u76d1\u89c6\u5668\u9501\u89c4\u5219&#xff0c;ReentrantLock\u9760volatile\u89c4\u5219\u3002<\/p>\n<h4>AQS\u7684\u5185\u5b58\u8bed\u4e49\u8bbe\u8ba1<\/h4>\n<p>AQS\u5de7\u5999\u5730\u7528volatile state\u4f5c\u4e3a&#034;\u5185\u5b58\u540c\u6b65\u951a\u70b9&#034;&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u8bbe\u8ba1&#xff1a;<\/p>\n<ul>\n<li>state\u7684volatile\u5199&#xff08;unlock\u65f6&#xff09;\u628a\u5f53\u524d\u7ebf\u7a0b\u5728\u4e34\u754c\u533a\u5185\u7684\u6240\u6709\u4fee\u6539\u5237\u65b0\u5230\u4e3b\u5185\u5b58<\/li>\n<li>state\u7684volatile\u8bfb&#xff08;lock\u65f6&#xff09;\u5f3a\u5236\u540e\u7eed\u8bfb\u53d6\u770b\u5230\u6700\u65b0\u503c<\/li>\n<\/ul>\n<p>\u8fd9\u7b49\u4ef7\u4e8e&#xff1a;unlock\u5177\u6709&#034;StoreStore &#043; StoreLoad&#034;\u7684\u8bed\u4e49&#xff08;volatile\u5199\u540e\u5c4f\u969c&#xff09;&#xff0c;lock\u5177\u6709&#034;LoadLoad &#043; LoadStore&#034;\u7684\u8bed\u4e49&#xff08;volatile\u8bfb\u540e\u5c4f\u969c&#xff09;\u3002\u4e0esynchronized\u7684&#034;\u89e3\u9501\u5199\u56de\u3001\u52a0\u9501\u6e05\u7a7a&#034;\u8bed\u4e49\u4e00\u81f4\u3002<\/p>\n<p>AQS\u7684\u5176\u4ed6\u540c\u6b65\u5668&#xff08;Semaphore\u3001CountDownLatch\u3001CyclicBarrier&#xff09;\u4e5f\u90fd\u57fa\u4e8evolatile state&#xff0c;\u56e0\u6b64\u5177\u6709\u76f8\u540c\u7684\u5185\u5b58\u8bed\u4e49\u2014\u2014\u91ca\u653e\/\u83b7\u53d6\u64cd\u4f5c\u4e4b\u95f4\u6709happens-before\u5173\u7cfb\u3002<\/p>\n<h3>CAS\u7684\u5185\u5b58\u8bed\u4e49<\/h3>\n<p>CAS&#xff08;Compare-And-Swap&#xff09;\u662fjava.util.concurrent.atomic\u5305\u539f\u5b50\u7c7b\u7684\u57fa\u7840\u64cd\u4f5c\u3002\u5b83\u4e0d\u4ec5\u4fdd\u8bc1\u539f\u5b50\u6027&#xff0c;\u8fd8\u6709\u660e\u786e\u7684\u5185\u5b58\u8bed\u4e49\u3002<\/p>\n<h4>CAS\u7684volatile\u8bfb\u5199\u8bed\u4e49<\/h4>\n<p>JMM\u89c4\u5b9a&#xff1a;CAS\u64cd\u4f5c\u540c\u65f6\u5177\u6709volatile\u8bfb\u548cvolatile\u5199\u7684\u5185\u5b58\u8bed\u4e49\u3002<\/p>\n<p>\u5177\u4f53\u6765\u8bf4&#xff0c;AtomicInteger.compareAndSet(int expect, int update)&#xff1a;<\/p>\n<ul>\n<li>\u8bfb\u5f53\u524d\u503c&#xff08;\u7c7b\u4f3cvolatile\u8bfb&#xff09;<\/li>\n<li>\u6bd4\u8f83\u662f\u5426\u7b49\u4e8eexpect<\/li>\n<li>\u5982\u679c\u76f8\u7b49&#xff0c;\u5199\u5165update&#xff08;\u7c7b\u4f3cvolatile\u5199&#xff09;<\/li>\n<li>\u6574\u4e2a\u8fc7\u7a0b\u539f\u5b50<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a&#034;volatile\u8bfb\u5199&#034;\u8bed\u4e49\u610f\u5473\u7740&#xff1a;<\/p>\n<ul>\n<li>CAS\u4e4b\u524d\u7684\u666e\u901a\u8bfb\u5199&#xff0c;\u4e0d\u4f1a\u91cd\u6392\u5230CAS\u4e4b\u540e&#xff08;volatile\u5199\u7684StoreStore\u6548\u679c&#xff09;<\/li>\n<li>CAS\u4e4b\u540e\u7684\u666e\u901a\u8bfb\u5199&#xff0c;\u4e0d\u4f1a\u91cd\u6392\u5230CAS\u4e4b\u524d&#xff08;volatile\u8bfb\u7684LoadLoad\/LoadStore\u6548\u679c&#xff09;<\/li>\n<li>CAS\u7684\u7ed3\u679c\u5bf9\u540e\u7eed\u64cd\u4f5c\u7acb\u5373\u53ef\u89c1&#xff08;volatile\u5199\u7684StoreLoad\u6548\u679c&#xff09;<\/li>\n<\/ul>\n<h4>\u5e95\u5c42\u5b9e\u73b0&#xff1a;Unsafe &#043; lock cmpxchg<\/h4>\n<p>CAS\u5728x86\u4e0a\u901a\u8fc7lock cmpxchg\u6307\u4ee4\u5b9e\u73b0\u3002lock\u524d\u7f00\u7684\u4f5c\u7528\u4e0a\u4e00\u7bc7\u8bb2\u8fc7\u2014\u2014\u89e6\u53d1\u7f13\u5b58\u4e00\u81f4\u6027\u534f\u8bae&#xff0c;\u4fdd\u8bc1\u539f\u5b50\u6027\u548c\u53ef\u89c1\u6027\u3002cmpxchg\u662f&#034;\u6bd4\u8f83\u5e76\u4ea4\u6362&#034;\u6307\u4ee4&#xff0c;\u914d\u5408lock\u524d\u7f00\u6210\u4e3a\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<p><span class=\"token comment\">\/\/ AtomicInteger.compareAndSet \u7684\u5e95\u5c42&#xff08;\u7b80\u5316&#xff09;<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token keyword\">boolean<\/span> <span class=\"token function\">compareAndSet<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> expect<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> update<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8c03\u7528 Unsafe.compareAndSwapInt<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6700\u7ec8\u751f\u6210 lock cmpxchg \u6307\u4ee4<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> unsafe<span class=\"token punctuation\">.<\/span><span class=\"token function\">compareAndSwapInt<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">,<\/span> valueOffset<span class=\"token punctuation\">,<\/span> expect<span class=\"token punctuation\">,<\/span> update<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>lock cmpxchg\u540c\u65f6\u5b8c\u6210&#034;\u8bfb-\u6bd4\u8f83-\u5199&#034;\u4e09\u6b65&#xff0c;\u4e14\u662f\u539f\u5b50\u7684&#xff0c;\u540c\u65f6\u5e26\u6709lock\u524d\u7f00\u7684\u5185\u5b58\u5c4f\u969c\u6548\u679c\u3002\u6240\u4ee5CAS\u7684\u5185\u5b58\u8bed\u4e49\u662fvolatile\u8bfb\u5199\u7684\u8d85\u96c6\u2014\u2014\u65e2\u539f\u5b50\u53c8\u53ef\u89c1\u3002<\/p>\n<h4>\u4ee3\u7801\u793a\u4f8b<\/h4>\n<p><span class=\"token comment\">\/\/ \u9002\u7528 JDK 8\/11\/17<\/span><br \/>\n<span class=\"token keyword\">import<\/span> <span class=\"token import\"><span class=\"token namespace\">java<span class=\"token punctuation\">.<\/span>util<span class=\"token punctuation\">.<\/span>concurrent<span class=\"token punctuation\">.<\/span>atomic<span class=\"token punctuation\">.<\/span><\/span><span class=\"token class-name\">AtomicInteger<\/span><\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">CASMemoryDemo<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token class-name\">AtomicInteger<\/span> atomicValue <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">AtomicInteger<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">int<\/span> normalData <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7ebf\u7a0bA&#xff1a;\u5148\u5199\u666e\u901a\u53d8\u91cf&#xff0c;\u518dCAS<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">writer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        normalData <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>                              <span class=\"token comment\">\/\/ \u64cd\u4f5c1&#xff1a;\u666e\u901a\u5199<\/span><br \/>\n        <span class=\"token keyword\">boolean<\/span> success <span class=\"token operator\">&#061;<\/span> atomicValue<span class=\"token punctuation\">.<\/span><span class=\"token function\">compareAndSet<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u64cd\u4f5c2&#xff1a;CAS<\/span><br \/>\n        <span class=\"token comment\">\/\/ CAS\u5177\u6709volatile\u5199\u7684\u8bed\u4e49<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u7a0b\u5e8f\u987a\u5e8f&#xff1a;\u64cd\u4f5c1 hb \u64cd\u4f5c2&#xff08;CAS\u524d\u7684\u5199\u4e0d\u80fd\u91cd\u6392\u5230CAS\u540e&#xff09;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7ebf\u7a0bB&#xff1a;\u5148CAS&#xff08;\u770b\u5230A\u7684CAS\u7ed3\u679c&#xff09;&#xff0c;\u518d\u8bfb\u666e\u901a\u53d8\u91cf<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">reader<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> val <span class=\"token operator\">&#061;<\/span> atomicValue<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u64cd\u4f5c3&#xff1a;volatile\u8bfb<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>val <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ CAS(volatile\u5199) hb volatile\u8bfb \u2192 \u64cd\u4f5c2 hb \u64cd\u4f5c3<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u4f20\u9012\u6027&#xff1a;\u64cd\u4f5c1 hb \u64cd\u4f5c3 hb \u64cd\u4f5c4<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>normalData<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u64cd\u4f5c4&#xff1a;\u8bfb\u523042<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u63a8\u7406\u94fe&#xff1a;<\/p>\n<li>\u7a0b\u5e8f\u987a\u5e8f&#xff1a;normalData&#061;42 hb CAS(0\u21921)<\/li>\n<li>volatile\u89c4\u5219&#xff1a;CAS(0\u21921)&#xff08;volatile\u5199\u8bed\u4e49&#xff09;hb atomicValue.get()&#xff08;volatile\u8bfb&#xff09;<\/li>\n<li>\u7a0b\u5e8f\u987a\u5e8f&#xff1a;get() hb \u8bfbnormalData<\/li>\n<li>\u4f20\u9012\u6027&#xff1a;normalData&#061;42 hb \u8bfbnormalData<\/li>\n<p>\u6240\u4ee5\u7ebf\u7a0bB\u8bfb\u5230val&#061;&#061;1\u65f6&#xff0c;normalData\u5fc5\u7136\u662f42\u3002\u8fd9\u5c31\u662fCAS\u7684\u5185\u5b58\u8bed\u4e49\u2014\u2014\u5b83\u548cvolatile\u4e00\u6837&#xff0c;\u53ef\u4ee5\u4f5c\u4e3a&#034;\u53d1\u5e03\u951a\u70b9&#034;\u3002<\/p>\n<h4>AtomicInteger.get()\u7684\u8bed\u4e49<\/h4>\n<p>\u6ce8\u610f\u4e0a\u9762\u7528\u4e86atomicValue.get()\u800c\u975e\u53e6\u4e00\u4e2aCAS\u3002AtomicInteger.get()\u5c31\u662f\u8bfb\u53d6volatile\u5b57\u6bb5\u2014\u2014AtomicInteger\u5185\u90e8value\u662fvolatile&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">AtomicInteger<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">volatile<\/span> <span class=\"token keyword\">int<\/span> value<span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ volatile&#xff01;<\/span><\/p>\n<p>    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> value<span class=\"token punctuation\">;<\/span>             <span class=\"token comment\">\/\/ volatile\u8bfb<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6240\u4ee5AtomicInteger\u7684\u6240\u6709\u64cd\u4f5c&#xff08;get\u3001set\u3001compareAndSet\u3001getAndIncrement&#xff09;\u90fd\u57fa\u4e8evolatile value&#xff0c;\u5177\u6709volatile\u7684\u5185\u5b58\u8bed\u4e49\u3002\u8fd9\u8ba9\u539f\u5b50\u7c7b\u65e2\u80fd\u4fdd\u8bc1\u539f\u5b50\u6027&#xff0c;\u53c8\u80fd\u4fdd\u8bc1\u53ef\u89c1\u6027\u3002<\/p>\n<h3>\u4e09\u79cd\u540c\u6b65\u65b9\u5f0f\u7684\u5185\u5b58\u8bed\u4e49\u5bf9\u6bd4<\/h3>\n<p>synchronized\u3001ReentrantLock\u3001CAS&#xff08;\u539f\u5b50\u7c7b&#xff09;\u662fJava\u5e76\u53d1\u7684\u4e09\u5927\u540c\u6b65\u624b\u6bb5\u3002\u5b83\u4eec\u7684\u5185\u5b58\u8bed\u4e49\u5bf9\u6bd4\u5982\u4e0b&#xff1a;<\/p>\n<h4>\u5bf9\u6bd4\u8868<\/h4>\n<table>\n<tr>\u7ef4\u5ea6synchronizedReentrantLockCAS&#xff08;AtomicInteger\u7b49&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u4e92\u65a5\u6027<\/td>\n<td>\u4e92\u65a5&#xff08;\u540c\u4e00\u65f6\u523b\u4e00\u4e2a\u7ebf\u7a0b&#xff09;<\/td>\n<td>\u4e92\u65a5<\/td>\n<td>\u65e0\u4e92\u65a5&#xff08;\u4e50\u89c2\u7b56\u7565&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u539f\u5b50\u6027<\/td>\n<td>\u4e34\u754c\u533a\u6574\u4f53\u539f\u5b50<\/td>\n<td>\u4e34\u754c\u533a\u6574\u4f53\u539f\u5b50<\/td>\n<td>\u5355\u4e2aCAS\u64cd\u4f5c\u539f\u5b50<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u89c1\u6027<\/td>\n<td>\u52a0\u9501\u6e05\u7a7a\u3001\u89e3\u9501\u5199\u56de<\/td>\n<td>volatile state&#xff08;\u7b49\u4ef7&#xff09;<\/td>\n<td>volatile\u8bfb\u5199\u8bed\u4e49<\/td>\n<\/tr>\n<tr>\n<td>\u6709\u5e8f\u6027<\/td>\n<td>\u4e34\u754c\u533a\u6574\u4f53\u6709\u5e8f<\/td>\n<td>volatile\u5c4f\u969c&#xff08;\u7b49\u4ef7&#xff09;<\/td>\n<td>CAS\u524d\u540e\u5c4f\u969c<\/td>\n<\/tr>\n<tr>\n<td>happens-before<\/td>\n<td>\u76d1\u89c6\u5668\u9501\u89c4\u5219&#xff08;unlock hb lock&#xff09;<\/td>\n<td>volatile\u89c4\u5219&#xff08;\u5199state hb \u8bfbstate&#xff09;<\/td>\n<td>volatile\u89c4\u5219&#xff08;CAS hb \u540e\u7eed\u8bfb&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5e95\u5c42\u6307\u4ee4<\/td>\n<td>monitorenter\/monitorexit<\/td>\n<td>CAS &#043; volatile state<\/td>\n<td>lock cmpxchg<\/td>\n<\/tr>\n<tr>\n<td>\u963b\u585e<\/td>\n<td>\u7ade\u4e89\u65f6\u963b\u585e&#xff08;\u9501\u5347\u7ea7&#xff09;<\/td>\n<td>\u7ade\u4e89\u65f6\u963b\u585e&#xff08;park\/unpark&#xff09;<\/td>\n<td>\u4e0d\u963b\u585e&#xff08;\u81ea\u65cb\u91cd\u8bd5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u4e34\u754c\u533a\u4fdd\u62a4\u3001\u590d\u5408\u64cd\u4f5c<\/td>\n<td>\u53ef\u4e2d\u65ad\/\u8d85\u65f6\/\u516c\u5e73\u9501<\/td>\n<td>\u5355\u53d8\u91cf\u539f\u5b50\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd&#xff08;\u4f4e\u7ade\u4e89&#xff09;<\/td>\n<td>\u504f\u5411\u9501\u51e0\u4e4e\u514d\u8d39<\/td>\n<td>\u7565\u6162\u4e8e\u504f\u5411\u9501<\/td>\n<td>\u63a5\u8fd1volatile\u8bfb\u5199<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd&#xff08;\u9ad8\u7ade\u4e89&#xff09;<\/td>\n<td>\u91cd\u91cf\u7ea7\u9501\u5f00\u9500\u5927<\/td>\n<td>\u4e0esynchronized\u76f8\u5f53<\/td>\n<td>\u81ea\u65cb\u6d88\u8017CPU<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u5185\u5b58\u8bed\u4e49\u7684\u7b49\u4ef7\u6027<\/h4>\n<p>\u4e09\u79cd\u65b9\u5f0f\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1\u662f\u7b49\u4ef7\u7684\u2014\u2014\u90fd\u5efa\u7acb\u4e86&#034;\u524d\u4e00\u4e2a\u64cd\u4f5c\u7684\u7ed3\u679c\u5bf9\u540e\u7eed\u64cd\u4f5c\u53ef\u89c1&#034;\u7684happens-before\u5173\u7cfb\u3002\u533a\u522b\u5728\u4e8e&#xff1a;<\/p>\n<ul>\n<li>synchronized&#xff1a;\u901a\u8fc7JVM\u5185\u7f6e\u7684monitorenter\/monitorexit\u5b9e\u73b0&#xff0c;\u52a0\u9501\u65f6\u6e05\u7a7a\u5de5\u4f5c\u5185\u5b58\u3001\u89e3\u9501\u65f6\u5199\u56de\u3002\u8bed\u4e49\u6700\u76f4\u89c2<\/li>\n<li>ReentrantLock&#xff1a;\u901a\u8fc7AQS\u7684volatile state\u5b9e\u73b0&#xff0c;lock\/unlock\u64cd\u4f5cstate&#xff0c;\u501f\u7528volatile\u7684\u5185\u5b58\u8bed\u4e49\u3002\u8bed\u4e49\u7b49\u4ef7\u4f46\u5b9e\u73b0\u4e0d\u540c<\/li>\n<li>CAS&#xff1a;\u901a\u8fc7lock cmpxchg\u6307\u4ee4&#xff0c;\u5355\u6b21\u64cd\u4f5c\u540c\u65f6\u5b8c\u6210&#034;\u8bfb-\u6bd4\u8f83-\u5199&#034;\u5e76\u4fdd\u8bc1\u53ef\u89c1\u6027\u3002\u7c92\u5ea6\u6700\u7ec6<\/li>\n<\/ul>\n<h4>\u9009\u62e9\u7b56\u7565<\/h4>\n<p><span class=\"token comment\">\/\/ \u9002\u7528 JDK 8\/11\/17<\/span><br \/>\n<span class=\"token comment\">\/\/ \u573a\u666f1&#xff1a;\u5355\u53d8\u91cf\u539f\u5b50\u64cd\u4f5c \u2192 \u7528\u539f\u5b50\u7c7b<\/span><br \/>\n<span class=\"token class-name\">AtomicInteger<\/span> counter <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">AtomicInteger<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\ncounter<span class=\"token punctuation\">.<\/span><span class=\"token function\">incrementAndGet<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ CAS&#xff0c;\u65e0\u9501&#xff0c;\u9ad8\u5e76\u53d1\u53cb\u597d<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u573a\u666f2&#xff1a;\u591a\u6b65\u590d\u5408\u64cd\u4f5c \u2192 \u7528synchronized\u6216ReentrantLock<\/span><br \/>\n<span class=\"token keyword\">synchronized<\/span> <span class=\"token punctuation\">(<\/span>lock<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>balance <span class=\"token operator\">&gt;&#061;<\/span> amount<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        balance <span class=\"token operator\">-&#061;<\/span> amount<span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">\/\/ \u591a\u6b65\u64cd\u4f5c\u9700\u8981\u6574\u4f53\u539f\u5b50<\/span><br \/>\n        <span class=\"token function\">transfer<\/span><span class=\"token punctuation\">(<\/span>amount<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u573a\u666f3&#xff1a;\u9700\u8981\u53ef\u4e2d\u65ad\/\u8d85\u65f6\/\u516c\u5e73 \u2192 \u7528ReentrantLock<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">tryLock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span><span class=\"token constant\">SECONDS<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u4e34\u754c\u533a<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">finally<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u573a\u666f4&#xff1a;\u7b80\u5355\u4e92\u65a5&#xff0c;\u4e0d\u9700\u8981\u9ad8\u7ea7\u529f\u80fd \u2192 \u4f18\u5148synchronized<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4ee3\u7801\u7b80\u6d01&#xff0c;JVM\u4f18\u5316\u5145\u5206&#xff08;\u504f\u5411\u9501\/\u8f7b\u91cf\u9501\/\u91cd\u91cf\u9501\u81ea\u52a8\u5347\u7ea7&#xff09;<\/span><\/p>\n<p>\u9009\u62e9\u539f\u5219&#xff1a;<\/p>\n<li>\u5355\u53d8\u91cf\u539f\u5b50\u64cd\u4f5c&#xff08;\u8ba1\u6570\u3001\u6807\u5fd7\u4f4d&#xff09;\u2192 \u539f\u5b50\u7c7b&#xff08;CAS&#xff09;&#xff0c;\u6027\u80fd\u6700\u4f18<\/li>\n<li>\u7b80\u5355\u590d\u5408\u64cd\u4f5c&#xff08;\u68c0\u67e5-\u6267\u884c-\u4fee\u6539&#xff09;\u2192 synchronized&#xff0c;\u4ee3\u7801\u7b80\u6d01<\/li>\n<li>\u9700\u8981\u9ad8\u7ea7\u7279\u6027&#xff08;\u53ef\u4e2d\u65ad\u3001\u8d85\u65f6\u3001\u516c\u5e73\u3001\u591aCondition&#xff09;\u2192 ReentrantLock<\/li>\n<li>\u4e0d\u786e\u5b9a\u65f6\u4f18\u5148synchronized\u2014\u2014JDK 6\u540e\u9501\u4f18\u5316\u5145\u5206&#xff0c;\u6027\u80fd\u901a\u5e38\u8db3\u591f&#xff0c;\u4e14\u4e0d\u6613\u51fa\u9519&#xff08;\u81ea\u52a8\u91ca\u653e\u9501&#xff09;<\/li>\n<h4>\u9501\u91ca\u653e-\u83b7\u53d6\u7684happens-before\u5173\u7cfb<\/h4>\n<p>\u65e0\u8bba\u54ea\u79cd\u9501&#xff0c;&#034;\u91ca\u653e-\u83b7\u53d6&#034;\u7684happens-before\u5173\u7cfb\u90fd\u662f\u5185\u5b58\u8bed\u4e49\u7684\u6838\u5fc3&#xff1a;<\/p>\n<p>\u7ebf\u7a0bA&#xff08;\u6301\u9501\u4fee\u6539&#xff09;:<br \/>\n   [\u4e34\u754c\u533a\u5185\u7684\u6240\u6709\u5199] \u2192 \u91ca\u653e\u9501<br \/>\n                         \u2502<br \/>\n                         \u2502 happens-before&#xff08;\u9501\u91ca\u653e-\u83b7\u53d6&#xff09;<br \/>\n                         \u2193<br \/>\n\u7ebf\u7a0bB&#xff08;\u83b7\u53d6\u9501\u8bfb\u53d6&#xff09;:<br \/>\n   \u83b7\u53d6\u9501 \u2192 [\u4e34\u754c\u533a\u5185\u7684\u6240\u6709\u8bfb\u80fd\u770b\u5230A\u7684\u5199]<\/p>\n<ul>\n<li>synchronized&#xff1a;unlock hb lock&#xff08;\u76d1\u89c6\u5668\u9501\u89c4\u5219&#xff09;<\/li>\n<li>ReentrantLock&#xff1a;unlock&#xff08;\u5199volatile state&#xff09;hb lock&#xff08;\u8bfbvolatile state&#xff09;&#xff08;volatile\u89c4\u5219&#xff09;<\/li>\n<li>AQS\u540c\u6b65\u5668&#xff08;Semaphore\/CountDownLatch&#xff09;&#xff1a;release&#xff08;\u5199volatile state&#xff09;hb acquire&#xff08;\u8bfbvolatile state&#xff09;<\/li>\n<\/ul>\n<p>\u8fd9\u6761&#034;\u9501\u91ca\u653e-\u83b7\u53d6&#034;\u7684happens-before\u5173\u7cfb&#xff0c;\u662f\u6240\u6709\u9501\u673a\u5236\u5171\u4eab\u7684\u5185\u5b58\u8bed\u4e49\u57fa\u77f3\u3002\u7406\u89e3\u4e86\u5b83&#xff0c;\u5c31\u80fd\u63a8\u5bfc\u51fa\u4efb\u4f55\u9501\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1\u3002<\/p>\n<h3>\u5b9e\u8df5\u8981\u70b9<\/h3>\n<li>\n<p>synchronized\u5757\u5185\u7684\u4fee\u6539\u5bf9\u4e0b\u4e00\u4e2a\u83b7\u53d6\u9501\u7684\u7ebf\u7a0b\u53ef\u89c1\u3002\u4e0d\u9700\u8981\u989d\u5916\u7528volatile\u4fee\u9970\u4e34\u754c\u533a\u5185\u7684\u53d8\u91cf\u2014\u2014\u9501\u7684\u5185\u5b58\u8bed\u4e49\u5df2\u8986\u76d6\u3002\u4f46\u5982\u679c\u67d0\u53d8\u91cf\u5728\u4e0d\u52a0\u9501\u65f6\u4e5f\u88ab\u8bbf\u95ee&#xff0c;\u90a3\u5b83\u9700\u8981\u81ea\u5df1\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1&#xff08;volatile\u6216\u9501&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>ReentrantLock\u7684lock\/unlock\u5fc5\u987b\u914d\u5bf9\u3002unlock\u4e00\u5b9a\u8981\u5728finally\u5757\u4e2d&#xff0c;\u5426\u5219\u5f02\u5e38\u65f6\u9501\u4e0d\u91ca\u653e&#xff0c;\u4e14\u5185\u5b58\u540c\u6b65&#xff08;\u5199\u56de\u4e3b\u5185\u5b58&#xff09;\u4e5f\u4e0d\u4f1a\u6267\u884c\u2014\u2014\u540e\u7eed\u83b7\u53d6\u9501\u7684\u7ebf\u7a0b\u53ef\u80fd\u770b\u4e0d\u5230\u6b63\u786e\u72b6\u6001\u3002<\/p>\n<\/li>\n<li>\n<p>\u539f\u5b50\u7c7b\u4e0d\u662f\u9501\u7684\u66ff\u4ee3\u54c1\u3002CAS\u9002\u5408\u5355\u53d8\u91cf\u7684\u539f\u5b50\u64cd\u4f5c&#xff0c;\u4f46\u4e0d\u80fd\u4fdd\u62a4\u591a\u6b65\u590d\u5408\u64cd\u4f5c\u3002AtomicInteger\u80fd\u4fdd\u8bc1incrementAndGet\u539f\u5b50&#xff0c;\u4f46&#034;\u68c0\u67e5\u4f59\u989d&#043;\u6263\u6b3e&#043;\u8f6c\u8d26&#034;\u8fd9\u79cd\u591a\u6b65\u64cd\u4f5c\u4ecd\u9700\u9501\u3002<\/p>\n<\/li>\n<li>\n<p>\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501\u7684\u5185\u5b58\u8bed\u4e49\u76f8\u540c\u3002ReentrantLock\u7684\u516c\u5e73\/\u975e\u516c\u5e73\u53ea\u5f71\u54cd\u83b7\u53d6\u9501\u7684\u987a\u5e8f&#xff08;\u662f\u5426\u5141\u8bb8\u63d2\u961f&#xff09;&#xff0c;\u4e0d\u5f71\u54cd\u5185\u5b58\u8bed\u4e49\u2014\u2014\u4e24\u8005\u90fd\u57fa\u4e8evolatile state&#xff0c;\u53ef\u89c1\u6027\u4fdd\u8bc1\u4e00\u81f4\u3002<\/p>\n<\/li>\n<li>\n<p>\u9501\u7684\u7c92\u5ea6\u5f71\u54cd\u5185\u5b58\u540c\u6b65\u5f00\u9500\u3002synchronized\u5757\u8d8a\u5927&#xff0c;\u6e05\u7a7a\/\u5199\u56de\u7684\u5de5\u4f5c\u5185\u5b58\u8303\u56f4\u8d8a\u5927\u3002\u4f46\u8fd9\u901a\u5e38\u4e0d\u662f\u74f6\u9888\u2014\u2014\u9501\u7ade\u4e89\u672c\u8eab\u7684\u5f00\u9500\u8fdc\u5927\u4e8e\u5185\u5b58\u540c\u6b65\u3002\u4f18\u5316\u5e94\u805a\u7126\u51cf\u5c11\u9501\u7ade\u4e89&#xff08;\u51cf\u5c0f\u7c92\u5ea6\u3001\u5206\u6bb5\u9501\u3001\u65e0\u9501\u5316&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>synchronized\u548cReentrantLock\u4e0d\u8981\u6df7\u7528\u4fdd\u62a4\u540c\u4e00\u8d44\u6e90\u3002\u4e24\u8005\u662f\u4e0d\u540c\u7684\u9501\u5bf9\u8c61&#xff08;synchronized\u7528\u5bf9\u8c61\u5934&#xff0c;ReentrantLock\u7528AQS&#xff09;&#xff0c;\u6df7\u7528\u4e0d\u6784\u6210&#034;\u540c\u4e00\u628a\u9501&#034;&#xff0c;\u76d1\u89c6\u5668\u9501\u89c4\u5219\u4e0d\u6210\u7acb&#xff0c;\u53ef\u89c1\u6027\u65e0\u4fdd\u8bc1\u3002\u9009\u62e9\u4e00\u79cd\u5e76\u7edf\u4e00\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>CAS\u81ea\u65cb\u5728\u9ad8\u7ade\u4e89\u4e0b\u6d88\u8017CPU\u3002\u539f\u5b50\u7c7b\u5728\u4f4e\u7ade\u4e89\u4e0b\u6027\u80fd\u4f18\u5f02&#xff0c;\u4f46\u9ad8\u7ade\u4e89\u65f6CAS\u9891\u7e41\u5931\u8d25\u91cd\u8bd5&#xff0c;\u6d6a\u8d39CPU\u5468\u671f\u3002\u8fd9\u79cd\u573a\u666f\u7528LongAdder&#xff08;JDK 8&#043;&#xff0c;\u5206\u6563\u70ed\u70b9&#xff09;\u6216\u6539\u7528\u9501\u66f4\u4f18\u3002<\/p>\n<\/li>\n<li>\n<p>AQS\u540c\u6b65\u5668&#xff08;Semaphore\/CountDownLatch&#xff09;\u4e5f\u6709\u5185\u5b58\u8bed\u4e49\u3002\u5b83\u4eec\u7684release\/acquire\u57fa\u4e8evolatile state&#xff0c;\u5177\u6709\u4e0e\u9501\u76f8\u540c\u7684happens-before\u5173\u7cfb\u3002CountDownLatch.countDown() hb await()\u8fd4\u56de&#xff0c;Semaphore.release() hb acquire()\u3002\u5584\u7528\u8fd9\u4e9b&#034;\u514d\u8d39&#034;\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1\u3002<\/p>\n<\/li>\n<h3>\u5c0f\u7ed3<\/h3>\n<ul>\n<li>synchronized\u7684\u5185\u5b58\u8bed\u4e49&#xff1a;\u52a0\u9501\u65f6\u6e05\u7a7a\u5de5\u4f5c\u5185\u5b58&#xff08;\u5f3a\u5236\u4ece\u4e3b\u5185\u5b58\u8bfb&#xff09;&#xff0c;\u89e3\u9501\u65f6\u5199\u56de\u4e3b\u5185\u5b58&#xff08;StoreStore &#043; StoreLoad&#xff09;\u3002\u7b49\u4ef7\u4e8e\u5728\u4e34\u754c\u533a\u8fb9\u754c\u63d2\u4e86\u5168\u529f\u80fd\u5185\u5b58\u5c4f\u969c<\/li>\n<li>ReentrantLock\u7684\u5185\u5b58\u8bed\u4e49&#xff1a;\u57fa\u4e8eAQS\u7684volatile state\u5b9e\u73b0\u3002lock\u662fCAS&#xff08;volatile\u8bfb&#043;\u5199&#xff09;&#xff0c;unlock\u662fsetState&#xff08;volatile\u5199&#xff09;\u3002\u901a\u8fc7volatile\u89c4\u5219\u5efa\u7acb\u4e0esynchronized\u7b49\u4ef7\u7684happens-before\u5173\u7cfb<\/li>\n<li>CAS\u7684\u5185\u5b58\u8bed\u4e49&#xff1a;\u540c\u65f6\u5177\u6709volatile\u8bfb\u548cvolatile\u5199\u7684\u8bed\u4e49\u3002lock cmpxchg\u6307\u4ee4\u4fdd\u8bc1\u539f\u5b50\u6027&#043;\u53ef\u89c1\u6027&#xff0c;\u53ef\u4f5c\u4e3a&#034;\u53d1\u5e03\u951a\u70b9&#034;\u5efa\u7acbhappens-before\u94fe<\/li>\n<li>\u4e09\u79cd\u65b9\u5f0f\u7b49\u4ef7\u6027&#xff1a;synchronized&#xff08;\u76d1\u89c6\u5668\u9501\u89c4\u5219&#xff09;\u3001ReentrantLock&#xff08;volatile\u89c4\u5219&#xff09;\u3001CAS&#xff08;volatile\u89c4\u5219&#xff09;\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1\u7b49\u4ef7&#xff0c;\u90fd\u662f&#034;\u91ca\u653e-\u83b7\u53d6&#034;\u7684happens-before\u5173\u7cfb\u3002\u533a\u522b\u5728\u4e8e\u4e92\u65a5\u6027\u3001\u963b\u585e\u884c\u4e3a\u3001\u529f\u80fd\u7279\u6027<\/li>\n<li>\u9009\u62e9\u7b56\u7565&#xff1a;\u5355\u53d8\u91cf\u539f\u5b50\u7528CAS\u539f\u5b50\u7c7b&#xff0c;\u7b80\u5355\u4e92\u65a5\u7528synchronized&#xff0c;\u9700\u8981\u9ad8\u7ea7\u7279\u6027\u7528ReentrantLock\u3002\u4e0d\u786e\u5b9a\u65f6\u4f18\u5148synchronized\u2014\u2014\u7b80\u6d01\u4e14\u4f18\u5316\u5145\u5206<\/li>\n<li>\u9501\u91ca\u653e-\u83b7\u53d6\u662fhappens-before\u6838\u5fc3&#xff1a;\u6240\u6709\u9501\u673a\u5236&#xff08;\u5305\u62ecAQS\u540c\u6b65\u5668&#xff09;\u7684\u5185\u5b58\u8bed\u4e49&#xff0c;\u90fd\u5f52\u7ed3\u4e3a&#034;\u91ca\u653e\u64cd\u4f5chappens-before\u540e\u7eed\u83b7\u53d6\u64cd\u4f5c&#034;\u3002\u7406\u89e3\u8fd9\u4e00\u6761&#xff0c;\u5c31\u80fd\u63a8\u5bfc\u4efb\u4f55\u9501\u7684\u53ef\u89c1\u6027\u4fdd\u8bc1<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>46-\u9501\u7684\u5185\u5b58\u8bed\u4e49<br \/>\n\u5f15\u8a00<br \/>\n\u524d\u51e0\u7bc7\u6211\u4eec\u5206\u522b\u8bb2\u4e86volatile\u548cfinal\u7684\u5185\u5b58\u8bed\u4e49\u3002volatile\u7528\u5185\u5b58\u5c4f\u969c\u4fdd\u8bc1\u53ef\u89c1\u6027\u548c\u6709\u5e8f\u6027&#xff0c;final\u7528StoreStore\/LoadLoad\u5c4f\u969c\u4fdd\u8bc1\u521d\u59cb\u5316\u5b89\u5168\u3002\u4f46Java\u5e76\u53d1\u4e2d\u8fd8\u6709\u4e00\u7c7b\u6700\u5e38\u7528\u7684\u540c\u6b65\u624b\u6bb5\u2014\u2014\u9501\u3002<br \/>\n\u9501\u4e0d\u53ea\u662f\\&#8221;\u4e92\u65a5\\&#8221;\u5de5\u5177&#xff0c;\u5b83\u540c\u6837\u627f\u62c5\u5185\u5b58\u540c\u6b65\u804c\u8d23\u3002\u65e0\u8bba\u662f\u5185\u7f6e\u7684synchronized&#xff0c;\u8fd8\u662fjava.util.concurrent.locks.ReentrantLock&#xff0c;\u751a\u81f3<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[426],"topic":[],"class_list":["post-93095","post","type-post","status-publish","format-standard","hentry","category-server","tag-jvm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/93095.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"46-\u9501\u7684\u5185\u5b58\u8bed\u4e49 \u5f15\u8a00 \u524d\u51e0\u7bc7\u6211\u4eec\u5206\u522b\u8bb2\u4e86volatile\u548cfinal\u7684\u5185\u5b58\u8bed\u4e49\u3002volatile\u7528\u5185\u5b58\u5c4f\u969c\u4fdd\u8bc1\u53ef\u89c1\u6027\u548c\u6709\u5e8f\u6027&#xff0c;final\u7528StoreStore\/LoadLoad\u5c4f\u969c\u4fdd\u8bc1\u521d\u59cb\u5316\u5b89\u5168\u3002\u4f46Java\u5e76\u53d1\u4e2d\u8fd8\u6709\u4e00\u7c7b\u6700\u5e38\u7528\u7684\u540c\u6b65\u624b\u6bb5\u2014\u2014\u9501\u3002 \u9501\u4e0d\u53ea\u662f&quot;\u4e92\u65a5&quot;\u5de5\u5177&#xff0c;\u5b83\u540c\u6837\u627f\u62c5\u5185\u5b58\u540c\u6b65\u804c\u8d23\u3002\u65e0\u8bba\u662f\u5185\u7f6e\u7684synchronized&#xff0c;\u8fd8\u662fjava.util.concurrent.locks.ReentrantLock&#xff0c;\u751a\u81f3\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/93095.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-11T14:48:46+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/93095.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/93095.html\",\"name\":\"\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-11T14:48:46+00:00\",\"dateModified\":\"2026-08-11T14:48:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/93095.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/93095.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/93095.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/93095.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"46-\u9501\u7684\u5185\u5b58\u8bed\u4e49 \u5f15\u8a00 \u524d\u51e0\u7bc7\u6211\u4eec\u5206\u522b\u8bb2\u4e86volatile\u548cfinal\u7684\u5185\u5b58\u8bed\u4e49\u3002volatile\u7528\u5185\u5b58\u5c4f\u969c\u4fdd\u8bc1\u53ef\u89c1\u6027\u548c\u6709\u5e8f\u6027&#xff0c;final\u7528StoreStore\/LoadLoad\u5c4f\u969c\u4fdd\u8bc1\u521d\u59cb\u5316\u5b89\u5168\u3002\u4f46Java\u5e76\u53d1\u4e2d\u8fd8\u6709\u4e00\u7c7b\u6700\u5e38\u7528\u7684\u540c\u6b65\u624b\u6bb5\u2014\u2014\u9501\u3002 \u9501\u4e0d\u53ea\u662f\"\u4e92\u65a5\"\u5de5\u5177&#xff0c;\u5b83\u540c\u6837\u627f\u62c5\u5185\u5b58\u540c\u6b65\u804c\u8d23\u3002\u65e0\u8bba\u662f\u5185\u7f6e\u7684synchronized&#xff0c;\u8fd8\u662fjava.util.concurrent.locks.ReentrantLock&#xff0c;\u751a\u81f3","og_url":"https:\/\/www.wsisp.com\/helps\/93095.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-11T14:48:46+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/93095.html","url":"https:\/\/www.wsisp.com\/helps\/93095.html","name":"\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-11T14:48:46+00:00","dateModified":"2026-08-11T14:48:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/93095.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/93095.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/93095.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010JVM\u539f\u7406\u8be6\u89e3\u301146-\u9501\u7684\u5185\u5b58\u8bed\u4e49"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/93095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=93095"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/93095\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=93095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=93095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=93095"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=93095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}