{"id":970,"date":"2022-07-27T11:32:14","date_gmt":"2022-07-27T03:32:14","guid":{"rendered":"https:\/\/blog.frost-s.com\/?p=970"},"modified":"2022-07-27T11:35:21","modified_gmt":"2022-07-27T03:35:21","slug":"java-%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%ef%bc%88%e4%b8%80%ef%bc%89volatile%e5%85%b3%e9%94%ae%e5%ad%97","status":"publish","type":"post","link":"https:\/\/blog.frost-s.com\/index.php\/2022\/07\/27\/java-%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%ef%bc%88%e4%b8%80%ef%bc%89volatile%e5%85%b3%e9%94%ae%e5%ad%97\/","title":{"rendered":"JAVA \u5e76\u53d1\u7f16\u7a0b\uff08\u4e00\uff09volatile\u5173\u952e\u5b57"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"anchor-0\"><a href=\"https:\/\/www.cnblogs.com\/zhongqifeng\/p\/14684028.html\" target=\"_blank\" rel=\"noreferrer noopener\">volatile\u8be6\u89e3<\/a><\/h1>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-1\">1\u3001\u6982\u5ff5<\/h1>\n\n\n\n<p>volatile\u662fJava\u4e2d\u7684\u5173\u952e\u5b57\uff0c\u7528\u6765\u4fee\u9970\u4f1a\u88ab\u4e0d\u540c\u7ebf\u7a0b\u8bbf\u95ee\u548c\u4fee\u6539\u7684\u53d8\u91cf\u3002JMM\uff08Java\u5185\u5b58\u6a21\u578b\uff09\u662f\u56f4\u7ed5\u5e76\u53d1\u8fc7\u7a0b\u4e2d\u5982\u4f55\u5904\u7406\u53ef\u89c1\u6027\u3001\u539f\u5b50\u6027\u548c\u6709\u5e8f\u6027\u8fd93\u4e2a\u7279\u5f81\u5efa\u7acb\u8d77\u6765\u7684\uff0c\u800cvolatile\u53ef\u4ee5\u4fdd\u8bc1\u5176\u4e2d\u7684\u4e24\u4e2a\u7279\u6027\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-2\">2\u3001Java\u5185\u5b58\u6a21\u578b\u76843\u4e2a\u7279\u6027<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"anchor-3\">1\uff09\u53ef\u89c1\u6027<\/h2>\n\n\n\n<p>\u53ef\u89c1\u6027\u662f\u4e00\u79cd\u590d\u6742\u7684\u5c5e\u6027\uff0c\u56e0\u4e3a\u53ef\u89c1\u6027\u4e2d\u7684\u9519\u8bef\u603b\u662f\u4f1a\u8fdd\u80cc\u6211\u4eec\u7684\u76f4\u89c9\u3002\u901a\u5e38\uff0c\u6211\u4eec\u65e0\u6cd5\u786e\u4fdd\u6267\u884c\u8bfb\u64cd\u4f5c\u7684\u7ebf\u7a0b\u80fd\u9002\u65f6\u5730\u770b\u5230\u5176\u4ed6\u7ebf\u7a0b\u5199\u5165\u7684\u503c\uff0c\u6709\u65f6\u751a\u81f3\u662f\u6839\u672c\u4e0d\u53ef\u80fd\u7684\u4e8b\u60c5\u3002\u4e3a\u4e86\u786e\u4fdd\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5bf9\u5185\u5b58\u5199\u5165\u64cd\u4f5c\u7684\u53ef\u89c1\u6027\uff0c\u5fc5\u987b\u4f7f\u7528\u540c\u6b65\u673a\u5236\u3002<\/p>\n\n\n\n<p>\u53ef\u89c1\u6027\uff0c\u662f\u6307\u7ebf\u7a0b\u4e4b\u95f4\u7684\u53ef\u89c1\u6027\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u7684\u72b6\u6001\u5bf9\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u662f\u53ef\u89c1\u7684\u3002\u4e5f\u5c31\u662f\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u7684\u7ed3\u679c\u3002\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u9a6c\u4e0a\u5c31\u80fd\u770b\u5230\u3002<\/p>\n\n\n\n<p>\u5728 Java \u4e2d volatile\u3001synchronized \u548c final \u90fd\u53ef\u4ee5\u5b9e\u73b0\u53ef\u89c1\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"anchor-4\">2\uff09\u539f\u5b50\u6027<\/h2>\n\n\n\n<p>\u539f\u5b50\u6027\u6307\u7684\u662f\u67d0\u4e2a\u7ebf\u7a0b\u6b63\u5728\u6267\u884c\u67d0\u4e2a\u64cd\u4f5c\u65f6\uff0c\u4e2d\u95f4\u4e0d\u53ef\u4ee5\u88ab\u52a0\u585e\u6216\u5206\u5272\uff0c\u8981\u4e48\u6574\u4f53\u6210\u529f\uff0c\u8981\u4e48\u6574\u4f53\u5931\u8d25\u3002\u6bd4\u5982 a=0\uff1b\uff08a\u975elong\u548cdouble\u7c7b\u578b\uff09 \u8fd9\u4e2a\u64cd\u4f5c\u662f\u4e0d\u53ef\u5206\u5272\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u8bf4\u8fd9\u4e2a\u64cd\u4f5c\u662f\u539f\u5b50\u64cd\u4f5c\u3002\u518d\u6bd4\u5982\uff1aa++\uff1b \u8fd9\u4e2a\u64cd\u4f5c\u5b9e\u9645\u662fa = a + 1\uff1b\u662f\u53ef\u5206\u5272\u7684\uff0c\u6240\u4ee5\u4ed6\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u3002\u975e\u539f\u5b50\u64cd\u4f5c\u90fd\u4f1a\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u9700\u8981\u6211\u4eec\u4f7f\u7528\u540c\u6b65\u6280\u672f\uff08sychronized\uff09\u6765\u8ba9\u5b83\u53d8\u6210\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u3002\u4e00\u4e2a\u64cd\u4f5c\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u90a3\u4e48\u6211\u4eec\u79f0\u5b83\u5177\u6709\u539f\u5b50\u6027\u3002Java\u7684 concurrent \u5305\u4e0b\u63d0\u4f9b\u4e86\u4e00\u4e9b\u539f\u5b50\u7c7b\uff0cAtomicInteger\u3001AtomicLong\u3001AtomicReference\u7b49\u3002<\/p>\n\n\n\n<p>\u5728 Java \u4e2d synchronized \u548c\u5728 lock\u3001unlock \u4e2d\u64cd\u4f5c\u4fdd\u8bc1\u539f\u5b50\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"anchor-5\">3\uff09\u6709\u5e8f\u6027<\/h2>\n\n\n\n<p>Java \u8bed\u8a00\u63d0\u4f9b\u4e86 volatile \u548c synchronized \u4e24\u4e2a\u5173\u952e\u5b57\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u4e4b\u95f4\u64cd\u4f5c\u7684\u6709\u5e8f\u6027\uff0cvolatile \u662f\u56e0\u4e3a\u5176\u672c\u8eab\u5305\u542b\u201c\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\u201d\u7684\u8bed\u4e49\uff0csynchronized \u662f\u7531\u201c\u4e00\u4e2a\u53d8\u91cf\u5728\u540c\u4e00\u4e2a\u65f6\u523b\u53ea\u5141\u8bb8\u4e00\u6761\u7ebf\u7a0b\u5bf9\u5176\u8fdb\u884c lock \u64cd\u4f5c\u201d\u8fd9\u6761\u89c4\u5219\u83b7\u5f97\u7684\uff0c\u6b64\u89c4\u5219\u51b3\u5b9a\u4e86\u6301\u6709\u540c\u4e00\u4e2a\u5bf9\u8c61\u9501\u7684\u4e24\u4e2a\u540c\u6b65\u5757\u53ea\u80fd\u4e32\u884c\u6267\u884c\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-6\">3\u3001volatile\u662fJava\u865a\u62df\u673a\u63d0\u4f9b\u7684\u8f7b\u91cf\u7ea7\u540c\u6b65\u673a\u5236\u3002<\/h1>\n\n\n\n<ul><li>\u4fdd\u8bc1\u53ef\u89c1\u6027<\/li><li>\u4e0d\u4fdd\u8bc1\u539f\u5b50\u6027<\/li><li>\u7981\u6b62\u6307\u4ee4\u91cd\u6392\uff08\u4fdd\u8bc1\u6709\u5e8f\u6027\uff09<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"anchor-7\">1\uff09\u53ef\u89c1\u6027\u9a8c\u8bc1<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.concurrent.TimeUnit;\n\n\/**\n * @author Frost\n * @date 2022\/7\/27 11:27\n *\/\n\nclass Test\n{\n    static class Data{\n        \/\/       volatile   int number =0;\n        volatile int number =0;\n        public void add()\n        {\n            this.number = number +1;\n        }\n    }\n\n    \/**\n     * \u542f\u52a8\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u4e00\u4e2awork\u7ebf\u7a0b\uff0c\u4e00\u4e2amain\u7ebf\u7a0b\uff0cwork\u7ebf\u7a0b\u4fee\u6539number\u503c\u540e\uff0c\u67e5\u770bmain\u7ebf\u7a0b\u7684number\n     *\/\n    private static void testVolatile() {\n        Data myData = new Data();\n\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName()+\"\\t come in\");\n            try {\n                TimeUnit.SECONDS.sleep(2);\n                myData.add();\n                System.out.println(Thread.currentThread().getName()+\"\\t update number value :\"+myData.number);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }, \"workThread\").start();\n\n        \/\/\u7b2c2\u4e2a\u7ebf\u7a0b\uff0cmain\u7ebf\u7a0b\n        while (myData.number == 0){\n            \/\/main\u7ebf\u7a0b\u8fd8\u5728\u627e0\n        }\n        System.out.println(Thread.currentThread().getName()+\"\\t mission is over\");\n        System.out.println(Thread.currentThread().getName()+\"\\t mission is over\uff0cmain get number is:\"+myData.number);\n    }\n    public static void main(String&#91;] args) {\n        testVolatile();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u5982\u4e0b\uff0c\u4f1a\u53d1\u73b0main\u7ebf\u7a0b\u6b7b\u5faa\u73af\uff0c\u8bf4\u660emain\u7ebf\u7a0b\u7684\u503c\u4e00\u76f4\u662f0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>workThread     come in\nworkThread     <strong>update<\/strong> <strong>number<\/strong> <strong>value<\/strong> :1<\/code><\/pre>\n\n\n\n<p>\u4fee\u6539volatile int number = 0,\u5373\u5728number\u524d\u52a0\u5173\u952e\u5b57volatile\uff0c\u91cd\u65b0\u8fd0\u884c\uff0cmain\u7ebf\u7a0b\u83b7\u53d6\u7ed3\u679c\u4e3a1\uff0c\u5faa\u73af\u7ed3\u675f\uff0c\u8f93\u5165\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>workThread     come <strong>in<\/strong>\nworkThread     update number value :1\nmain     mission <strong>is<\/strong> <strong>over<\/strong>\nmain     mission <strong>is<\/strong> <strong>over<\/strong>\uff0cmain <strong>get<\/strong> number <strong>is<\/strong>:1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"anchor-8\">2\uff09\u4e0d\u4fdd\u8bc1\u539f\u5b50\u6027\u9a8c\u8bc1<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> java.util.concurrent.TimeUnit;\n\n<strong>class<\/strong> <strong>T<\/strong>\n{\n    <strong>static<\/strong> <strong>class<\/strong> <strong>Data<\/strong>{\n       volatile   int number =0;\n        public void add()\n        {\n            this.number = number +1;\n        }\n    }\n    private <strong>static<\/strong> void testAtomic() throws InterruptedException {\n        Data myData = <strong>new<\/strong> Data();\n\n        <strong>for<\/strong> (int i = 0; i &lt; 10; i++) {\n            <strong>new<\/strong> Thread(() -&gt;{\n                <strong>for<\/strong> (int j = 0; j &lt; 1000; j++) {\n                    myData.add();\n                }\n            },\"addPlusThread:\"+ i).<strong>start<\/strong>();\n        }\n        \/\/\u7b49\u5f85\u4e0a\u8fb920\u4e2a\u7ebf\u7a0b\u7ed3\u675f\u540e(\u9884\u8ba15\u79d2\u80af\u5b9a\u7ed3\u675f\u4e86)\uff0c\u5728<strong>main<\/strong>\u7ebf\u7a0b\u4e2d\u83b7\u53d6\u6700\u540e\u7684<strong>number<\/strong>\n        <strong>TimeUnit<\/strong>.<strong>SECONDS<\/strong>.<strong>sleep<\/strong>(10);\n        <strong>while<\/strong> (Thread.activeCount() &gt; 2){\n            <strong>Thread<\/strong>.<strong>yield<\/strong>();\n        }\n        <strong>System<\/strong>.<strong>out<\/strong>.<strong>println<\/strong>(\"final value\uff1a\"+myData.number);\n    }\n    <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> <strong>main<\/strong>(String&#91;] args) <strong>throws<\/strong> <strong>InterruptedException<\/strong> {\n        <strong>testAtomic<\/strong>();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u7a0b\u5e8f\u540e\u53d1\u73b0\u8f93\u51fa\u503c\uff0c\u5e76\u4e0d\u4e00\u5b9a\u662f\u671f\u671b\u503c10000\uff0c\u5f80\u5f80\u662f\u6bd410000\u5c0f\u7684\u6570\u503c\u3002<\/p>\n\n\n\n<p>\u539f\u56e0\u662fi++\u5728\u8f6c\u5316\u4e3a\u5b57\u8282\u7801\u6307\u4ee4\u7684\u65f6\u5019\u662f4\u6761\u6307\u4ee4\u3002<\/p>\n\n\n\n<ul><li>getfield \u83b7\u53d6\u539f\u59cb\u503c<\/li><li>iconst_1 \u5c06\u503c\u5165\u6808<\/li><li>iadd \u8fdb\u884c\u52a0 1 \u64cd\u4f5c<\/li><li>putfield \u628a iadd \u540e\u7684\u64cd\u4f5c\u5199\u56de\u4e3b\u5185\u5b58<\/li><\/ul>\n\n\n\n<p>\u8fd9\u6837\u5728\u8fd0\u884c\u65f6\u5019\u5c31\u4f1a\u5b58\u5728\u591a\u7ebf\u7a0b\u7ade\u4e89\u95ee\u9898\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e86\u4e22\u5931\u5199\u503c\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u5982\u4f55\u89e3\u51b3\u539f\u5b50\u6027\u95ee\u9898\u5462\uff1f\u52a0&nbsp;<code>synchronized<\/code>&nbsp;\u6216\u8005\u76f4\u63a5\u4f7f\u7528&nbsp;<code>Automic<\/code>&nbsp;\u539f\u5b50\u7c7b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"anchor-9\">3\uff09\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u9a8c\u8bc1<\/h2>\n\n\n\n<p>\u8ba1\u7b97\u673a\u5728\u6267\u884c\u7a0b\u5e8f\u65f6\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u7f16\u8bd1\u5668\u548c\u5904\u7406\u5668\u5e38\u5e38\u4f1a\u5bf9\u6307\u4ee4\u505a\u91cd\u6392\uff0c\u4e00\u822c\u5206\u4e3a\u4ee5\u4e0b 3 \u79cd\uff1a<\/p>\n\n\n\n<p>\u5904\u7406\u5668\u5728\u8fdb\u884c\u91cd\u6392\u5e8f\u65f6\u5fc5\u987b\u8981\u8003\u8651\u6307\u4ee4\u4e4b\u95f4\u7684<strong>\u6570\u636e\u4f9d\u8d56\u6027<\/strong>\uff0c\u6211\u4eec\u53eb\u505a&nbsp;<code>as-if-serial<\/code>&nbsp;\u8bed\u4e49\u3002<\/p>\n\n\n\n<p>\u5355\u7ebf\u7a0b\u73af\u5883\u91cc\u786e\u4fdd\u7a0b\u5e8f\u6700\u7ec8\u6267\u884c\u7ed3\u679c\u548c\u4ee3\u7801\u987a\u5e8f\u6267\u884c\u7684\u7ed3\u679c\u4e00\u81f4\uff1b\u4f46\u662f\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c\uff0c\u7531\u4e8e\u7f16\u8bd1\u5668\u4f18\u5316\u91cd\u6392\u7684\u5b58\u5728\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u80fd\u5426\u4fdd\u8bc1\u4e00\u81f4\u6027\u662f\u65e0\u6cd5\u786e\u5b9a\u7684\uff0c\u7ed3\u679c\u65e0\u6cd5\u9884\u6d4b\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5f80\u5f80\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u9a8c\u8bc1 volatile \u7981\u6b62\u6307\u4ee4\u91cd\u6392\uff0c\u5982\u679c\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u6700\u540e\u7684\u8f93\u51fa\u7ed3\u679c\u4e0d\u4e00\u5b9a\u662f\u6211\u4eec\u60f3\u8c61\u5230\u7684 2\uff0c\u8fd9\u65f6\u5c31\u8981\u628a\u4e24\u4e2a\u53d8\u91cf\u90fd\u8bbe\u7f6e\u4e3a volatile\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>public<\/strong> <strong>class<\/strong> <strong>ReSortSeqDemo<\/strong> {\n\n    int a = 0;\n    boolean flag = false;\n\n    <strong>public<\/strong> <strong>void<\/strong> <strong>mehtod1<\/strong>(){\n        a = 1;\n        flag = true;\n    }\n\n    <strong>public<\/strong> <strong>void<\/strong> <strong>method2<\/strong>(){\n        <strong>if<\/strong>(flag){\n            a = a +1;\n            System.<strong>out<\/strong>.println(\"reorder value: \"+a);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><code>volatile<\/code>&nbsp;\u5b9e\u73b0\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u4f18\u5316\uff0c\u4ece\u800c\u907f\u514d\u4e86\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7a0b\u5e8f\u51fa\u73b0\u4e71\u5e8f\u6267\u884c\u7684\u73b0\u8c61\u3002<\/p>\n\n\n\n<p>\u8fd8\u6709\u4e00\u4e2a\u6211\u4eec\u6700\u5e38\u89c1\u7684\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d&nbsp;<code>DCL(double-checked locking)<\/code>&nbsp;\u7248\u672c\u7684\u5355\u4f8b\u6a21\u5f0f\u4e2d\uff0c\u5c31\u662f\u4f7f\u7528\u4e86 volatile \u7981\u6b62\u6307\u4ee4\u91cd\u6392\u7684\u7279\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>public<\/strong> <strong>class<\/strong> <strong>Singleton<\/strong> {\n\n    <strong>private<\/strong> <strong>static<\/strong> <strong>volatile<\/strong> Singleton instance;\n  \n    <strong>private<\/strong> <strong>Singleton<\/strong>(){}\n    \/\/ DCL\n    <strong>public<\/strong> <strong>static<\/strong> Singleton <strong>getInstance<\/strong>(){\n        <strong>if<\/strong>(instance ==null){   \/\/\u7b2c\u4e00\u6b21\u68c0\u67e5\n            synchronized (Singleton.<strong>class<\/strong>){\n                <strong>if<\/strong>(instance == null){   \/\/\u7b2c\u4e8c\u6b21\u68c0\u67e5\n                    instance = <strong>new<\/strong> Singleton();\n                }\n            }\n        }\n        <strong>return<\/strong> instance;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3a\u6709\u6307\u4ee4\u91cd\u6392\u5e8f\u7684\u5b58\u5728\uff0c\u53cc\u7aef\u68c0\u7d22\u673a\u5236\u4e5f\u4e0d\u4e00\u5b9a\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u56e0\u4e3a<code>instance = new Singleton();<\/code>&nbsp;\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u8fc7\u7a0b\u5176\u5b9e\u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u7684\u64cd\u4f5c\uff0c\u5b83\u4f1a\u5206\u4e3a\u4e09\u90e8\u5206\u6267\u884c\uff0c<\/p>\n\n\n\n<ul><li>\u7ed9 instance \u5206\u914d\u5185\u5b58<\/li><li>\u8c03\u7528 instance \u7684\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u5bf9\u8c61<\/li><li>\u5c06 instance \u5bf9\u8c61\u6307\u5411\u5206\u914d\u7684\u5185\u5b58\u7a7a\u95f4\uff08\u6267\u884c\u5b8c\u8fd9\u6b65 instance \u5c31\u4e3a\u975e null \u4e86\uff09<\/li><\/ul>\n\n\n\n<p>\u6b65\u9aa4 2 \u548c 3 \u4e0d\u5b58\u5728\u6570\u636e\u4f9d\u8d56\u5173\u7cfb\uff0c\u5982\u679c\u865a\u62df\u673a\u5b58\u5728\u6307\u4ee4\u91cd\u6392\u5e8f\u4f18\u5316\uff0c\u5219\u6b65\u9aa4 2\u548c 3 \u7684\u987a\u5e8f\u662f\u65e0\u6cd5\u786e\u5b9a\u7684\u3002\u5982\u679cA\u7ebf\u7a0b\u7387\u5148\u8fdb\u5165\u540c\u6b65\u4ee3\u7801\u5757\u5e76\u5148\u6267\u884c\u4e86 3 \u800c\u6ca1\u6709\u6267\u884c 2\uff0c\u6b64\u65f6\u56e0\u4e3a instance \u5df2\u7ecf\u975e null\u3002\u8fd9\u65f6\u5019\u7ebf\u7a0b B \u5728\u7b2c\u4e00\u6b21\u68c0\u67e5\u7684\u65f6\u5019\uff0c\u4f1a\u53d1\u73b0 instance \u5df2\u7ecf\u662f \u975enull \u4e86\uff0c\u5c31\u5c06\u5176\u8fd4\u56de\u4f7f\u7528\uff0c\u4f46\u662f\u6b64\u65f6 instance \u5b9e\u9645\u4e0a\u8fd8\u672a\u521d\u59cb\u5316\uff0c\u81ea\u7136\u5c31\u4f1a\u51fa\u9519\u3002\u6240\u4ee5\u6211\u4eec\u8981\u9650\u5236\u5b9e\u4f8b\u5bf9\u8c61\u7684\u6307\u4ee4\u91cd\u6392\uff0c\u7528 volatile \u4fee\u9970\uff08JDK 5 \u4e4b\u524d\u4f7f\u7528\u4e86 volatile \u7684\u53cc\u68c0\u9501\u662f\u6709\u95ee\u9898\u7684\uff09\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-10\">4\u3001\u539f\u7406<\/h1>\n\n\n\n<p>volatile \u53ef\u4ee5\u4fdd\u8bc1\u7ebf\u7a0b\u53ef\u89c1\u6027\u4e14\u63d0\u4f9b\u4e86\u4e00\u5b9a\u7684\u6709\u5e8f\u6027\uff0c\u4f46\u662f\u65e0\u6cd5\u4fdd\u8bc1\u539f\u5b50\u6027\u3002\u5728 JVM \u5e95\u5c42\u662f\u57fa\u4e8e\u5185\u5b58\u5c4f\u969c\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<ul><li>\u5f53\u5bf9\u975e volatile \u53d8\u91cf\u8fdb\u884c\u8bfb\u5199\u7684\u65f6\u5019\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5148\u4ece\u5185\u5b58\u62f7\u8d1d\u53d8\u91cf\u5230 CPU \u7f13\u5b58\u4e2d\u3002\u5982\u679c\u8ba1\u7b97\u673a\u6709\u591a\u4e2aCPU\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u5728\u4e0d\u540c\u7684 CPU \u4e0a\u88ab\u5904\u7406\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u62f7\u8d1d\u5230\u4e0d\u540c\u7684 CPU cache \u4e2d\u3002<\/li><li>\u800c\u58f0\u660e\u53d8\u91cf\u662f volatile \u7684\uff0cJVM \u4fdd\u8bc1\u4e86\u6bcf\u6b21\u8bfb\u53d8\u91cf\u90fd\u4ece\u5185\u5b58\u4e2d\u8bfb\uff0c\u8df3\u8fc7 CPU cache \u8fd9\u4e00\u6b65\uff0c\u6240\u4ee5\u5c31\u4e0d\u4f1a\u6709\u53ef\u89c1\u6027\u95ee\u9898\u3002<ul><li>\u5bf9 volatile \u53d8\u91cf\u8fdb\u884c\u5199\u64cd\u4f5c\u65f6\uff0c\u4f1a\u5728\u5199\u64cd\u4f5c\u540e\u52a0\u4e00\u6761 store \u5c4f\u969c\u6307\u4ee4\uff0c\u5c06\u5de5\u4f5c\u5185\u5b58\u4e2d\u7684\u5171\u4eab\u53d8\u91cf\u5237\u65b0\u56de\u4e3b\u5185\u5b58\uff1b<\/li><li>\u5bf9 volatile \u53d8\u91cf\u8fdb\u884c\u8bfb\u64cd\u4f5c\u65f6\uff0c\u4f1a\u5728\u5199\u64cd\u4f5c\u540e\u52a0\u4e00\u6761 load \u5c4f\u969c\u6307\u4ee4\uff0c\u4ece\u4e3b\u5185\u5b58\u4e2d\u8bfb\u53d6\u5171\u4eab\u53d8\u91cf\uff1b<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>\u901a\u8fc7 hsdis \u5de5\u5177\u83b7\u53d6 JIT \u7f16\u8bd1\u5668\u751f\u6210\u7684\u6c47\u7f16\u6307\u4ee4\u6765\u770b\u770b\u5bf9 volatile \u8fdb\u884c\u5199\u64cd\u4f5cCPU\u4f1a\u505a\u4ec0\u4e48\u4e8b\u60c5\uff0c\u8fd8\u662f\u7528\u4e0a\u8fb9\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u53ef\u4ee5\u770b\u5230<\/p>\n\n\n\n<p>\u6709 volatile \u4fee\u9970\u7684\u5171\u4eab\u53d8\u91cf\u8fdb\u884c\u5199\u64cd\u4f5c\u65f6\u4f1a\u591a\u51fa\u7b2c\u4e8c\u884c\u6c47\u7f16\u4ee3\u7801\uff0c\u8be5\u53e5\u4ee3\u7801\u7684\u610f\u601d\u662f<strong>\u5bf9\u539f\u503c\u52a0\u96f6<\/strong>\uff0c\u5176\u4e2d\u76f8\u52a0\u6307\u4ee4addl\u524d\u6709&nbsp;<strong>lock<\/strong>&nbsp;\u4fee\u9970\u3002\u901a\u8fc7\u67e5IA-32\u67b6\u6784\u8f6f\u4ef6\u5f00\u53d1\u8005\u624b\u518c\u53ef\u77e5\uff0clock\u524d\u7f00\u7684\u6307\u4ee4\u5728\u591a\u6838\u5904\u7406\u5668\u4e0b\u4f1a\u5f15\u53d1\u4e24\u4ef6\u4e8b\u60c5\uff1a<\/p>\n\n\n\n<ul><li>\u5c06\u5f53\u524d\u5904\u7406\u5668\u7f13\u5b58\u884c\u7684\u6570\u636e\u5199\u56de\u5230\u7cfb\u7edf\u5185\u5b58<\/li><li>\u8fd9\u4e2a\u5199\u56de\u5185\u5b58\u7684\u64cd\u4f5c\u4f1a\u5f15\u8d77\u5728\u5176\u4ed6CPU\u91cc\u7f13\u5b58\u4e86\u8be5\u5185\u5b58\u5730\u5740\u7684\u6570\u636e\u65e0\u6548<\/li><\/ul>\n\n\n\n<p><strong>\u6b63\u662f lock \u5b9e\u73b0\u4e86 volatile \u7684\u300c\u9632\u6b62\u6307\u4ee4\u91cd\u6392\u300d\u300c\u5185\u5b58\u53ef\u89c1\u300d\u7684\u7279\u6027<\/strong><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-11\">5\u3001\u4f7f\u7528\u573a\u666f<\/h1>\n\n\n\n<p>\u60a8\u53ea\u80fd\u5728\u6709\u9650\u7684\u4e00\u4e9b\u60c5\u5f62\u4e0b\u4f7f\u7528 volatile \u53d8\u91cf\u66ff\u4ee3\u9501\u3002\u8981\u4f7f volatile \u53d8\u91cf\u63d0\u4f9b\u7406\u60f3\u7684\u7ebf\u7a0b\u5b89\u5168\uff0c\u5fc5\u987b\u540c\u65f6\u6ee1\u8db3\u4e0b\u9762\u4e24\u4e2a\u6761\u4ef6\uff1a<\/p>\n\n\n\n<ul><li>\u5bf9\u53d8\u91cf\u7684\u5199\u64cd\u4f5c\u4e0d\u4f9d\u8d56\u4e8e\u5f53\u524d\u503c<\/li><li>\u8be5\u53d8\u91cf\u6ca1\u6709\u5305\u542b\u5728\u5177\u6709\u5176\u4ed6\u53d8\u91cf\u7684\u4e0d\u53d8\u5f0f\u4e2d<\/li><\/ul>\n\n\n\n<p>\u5176\u5b9e\u5c31\u662f\u5728\u9700\u8981\u4fdd\u8bc1\u539f\u5b50\u6027\u7684\u573a\u666f\uff0c\u4e0d\u8981\u4f7f\u7528 volatile\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"anchor-12\">6\u3001volatile\u6027\u80fd<\/h1>\n\n\n\n<p>volatile \u7684\u8bfb\u6027\u80fd\u6d88\u8017\u4e0e\u666e\u901a\u53d8\u91cf\u51e0\u4e4e\u76f8\u540c\uff0c\u4f46\u662f\u5199\u64cd\u4f5c\u7a0d\u6162\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5728\u672c\u5730\u4ee3\u7801\u4e2d\u63d2\u5165\u8bb8\u591a\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\u6765\u4fdd\u8bc1\u5904\u7406\u5668\u4e0d\u53d1\u751f\u4e71\u5e8f\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u5f15\u7528\u300a\u6b63\u786e\u4f7f\u7528 volaitle \u53d8\u91cf\u300b\u4e00\u6587\u4e2d\u7684\u8bdd\uff1a<\/p>\n\n\n\n<p>\u5f88\u96be\u505a\u51fa\u51c6\u786e\u3001\u5168\u9762\u7684\u8bc4\u4ef7\uff0c\u4f8b\u5982 \u201cX \u603b\u662f\u6bd4 Y \u5feb\u201d\uff0c\u5c24\u5176\u662f\u5bf9 JVM \u5185\u5728\u7684\u64cd\u4f5c\u800c\u8a00\u3002\uff08\u4f8b\u5982\uff0c\u67d0\u4e9b\u60c5\u51b5\u4e0b JVM \u4e5f\u8bb8\u80fd\u591f\u5b8c\u5168\u5220\u9664\u9501\u673a\u5236\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u96be\u4ee5\u62bd\u8c61\u5730\u6bd4\u8f83&nbsp;<code>volatile<\/code>&nbsp;\u548c&nbsp;<code>synchronized<\/code>&nbsp;\u7684\u5f00\u9500\u3002\uff09\u5c31\u662f\u8bf4\uff0c\u5728\u76ee\u524d\u5927\u591a\u6570\u7684\u5904\u7406\u5668\u67b6\u6784\u4e0a\uff0cvolatile \u8bfb\u64cd\u4f5c\u5f00\u9500\u975e\u5e38\u4f4e \u2014\u2014 \u51e0\u4e4e\u548c\u975e volatile \u8bfb\u64cd\u4f5c\u4e00\u6837\u3002\u800c volatile \u5199\u64cd\u4f5c\u7684\u5f00\u9500\u8981\u6bd4\u975e volatile \u5199\u64cd\u4f5c\u591a\u5f88\u591a\uff0c\u56e0\u4e3a\u8981\u4fdd\u8bc1\u53ef\u89c1\u6027\u9700\u8981\u5b9e\u73b0\u5185\u5b58\u754c\u5b9a\uff08Memory Fence\uff09\uff0c\u5373\u4fbf\u5982\u6b64\uff0cvolatile \u7684\u603b\u5f00\u9500\u4ecd\u7136\u8981\u6bd4\u9501\u83b7\u53d6\u4f4e\u3002<\/p>\n\n\n\n<p>volatile \u64cd\u4f5c\u4e0d\u4f1a\u50cf\u9501\u4e00\u6837\u9020\u6210\u963b\u585e\uff0c\u56e0\u6b64\uff0c\u5728\u80fd\u591f\u5b89\u5168\u4f7f\u7528 volatile \u7684\u60c5\u51b5\u4e0b\uff0cvolatile \u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e9b\u4f18\u4e8e\u9501\u7684\u53ef\u4f38\u7f29\u7279\u6027\u3002\u5982\u679c\u8bfb\u64cd\u4f5c\u7684\u6b21\u6570\u8981\u8fdc\u8fdc\u8d85\u8fc7\u5199\u64cd\u4f5c\uff0c\u4e0e\u9501\u76f8\u6bd4\uff0cvolatile \u53d8\u91cf\u901a\u5e38\u80fd\u591f\u51cf\u5c11\u540c\u6b65\u7684\u6027\u80fd\u5f00\u9500\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>volatile\u8be6\u89e3 1\u3001\u6982\u5ff5 volatile\u662fJava\u4e2d\u7684\u5173\u952e\u5b57\uff0c\u7528\u6765\u4fee\u9970\u4f1a\u88ab\u4e0d\u540c\u7ebf\u7a0b\u8bbf\u95ee\u548c\u4fee\u6539\u7684\u53d8\u91cf\u3002J [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":975,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,8],"tags":[40,33],"_links":{"self":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/970"}],"collection":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/comments?post=970"}],"version-history":[{"count":3,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/970\/revisions"}],"predecessor-version":[{"id":976,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/970\/revisions\/976"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media\/975"}],"wp:attachment":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media?parent=970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/categories?post=970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/tags?post=970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}