{"id":199786,"date":"2020-11-08T08:08:59","date_gmt":"2020-11-08T00:08:59","guid":{"rendered":"http:\/\/4563.org\/?p=199786"},"modified":"2020-11-08T08:08:59","modified_gmt":"2020-11-08T00:08:59","slug":"%e5%92%a8%e8%af%a2%e4%b8%80%e4%b8%aa%e5%85%b3%e4%ba%8e-synchronized-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=199786","title":{"rendered":"\u54a8\u8be2\u4e00\u4e2a\u5173\u4e8e synchronized \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u54a8\u8be2\u4e00\u4e2a\u5173\u4e8e synchronized \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : levizheng <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5355\u4f8b\u6a21\u5f0f\u6709\u4e00\u79cd \u53cc\u91cd\u68c0\u9a8c\u9501\u7684\u5199\u6cd5\uff0c\u5176\u4e2d volatile \u7528\u6765\u4fdd\u8bc1 Jit \u7f16\u8bd1\u5668\u8fdb\u884c\u6307\u4ee4\u91cd\u6392\uff0c\u4ee5\u9632\u5176\u4ed6\u7ebf\u7a0b\u83b7\u53d6\u4e86\u672a\u521d\u59cb\u5316\u7684\u5b9e\u4f8b\u3002<\/p>\n<p>\u6211\u7684\u95ee\u9898\u662f\uff1asynchronized \u5173\u952e\u5b57\u4e0d\u662f\u53ef\u4ee5\u4fdd\u8bc1\u7ebf\u7a0b\u7684\u53ef\u89c1\u6027\u3001\u539f\u5b50\u6027\u548c\u6709\u5e8f\u6027\u5417\uff1f\u4e0d\u662f\u5728\u6267\u884c\u5b8c\u540c\u6b65\u4ee3\u7801\u91ca\u653e\u9501\u4e4b\u524d\u5c06\u5de5\u4f5c\u5185\u5b58\u7684\u503c\u540c\u6b65\u5237\u65b0\u5230\u4e3b\u5185\u5b58\u4e2d\u5417\uff1f\u5982\u679c\u662f\u8fd9\u6837\u90a3\u4e48\u5176\u4ed6\u7ebf\u7a0b\u5728 2 \u6b21\u68c0\u6d4b\u7684\u65f6\u5019\u4e0d\u5e94\u8be5\u90fd\u662f\u62ff\u5230\u7684\u662f\u975e null \u7684\u4e14\u5df2\u7ecf\u521d\u59cb\u5316\u7684\u5b9e\u4f8b\u5417\uff1f\u4e3a\u4ec0\u4e48\u8fd8\u4f1a\u6709\u8fd9\u79cd\u60c5\u51b5\u5462\uff1f<\/p>\n<p>\u6000\u7591\u7684\u65b9\u5411\uff1a \u96be\u9053\u8bf4 synchronized \u5728\u91ca\u653e\u9501\u4e4b\u524d\u5c31\u5df2\u7ecf\u4f1a\u628a\u5de5\u4f5c\u5185\u5b58\u7684\u4e00\u4e9b\u503c\u540c\u6b65\u5230\u4e3b\u5185\u5b58\uff1f\u90a3\u540c\u6b65\u8fd8\u6709\u5565\u610f\u4e49\u5462\u3002<\/p>\n<pre><code>public class Singleton {    private volatile static Singleton uniqueInstance;    private Singleton() {}    public static Singleton getUniqueInstance() {      \/\/\u5148\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u5df2\u7ecf\u5b9e\u4f8b\u8fc7\uff0c\u6ca1\u6709\u5b9e\u4f8b\u5316\u8fc7\u624d\u8fdb\u5165\u52a0\u9501\u4ee3\u7801        if (uniqueInstance == null) {            \/\/\u7c7b\u5bf9\u8c61\u52a0\u9501            synchronized (Singleton.class) {                if (uniqueInstance == null) {                    uniqueInstance = new Singleton();                }            }        }       return uniqueInstance;   }  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>18<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4265662\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : aerzha <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u513f\u662f\u4e3a\u4e86\u9632\u6b62\u6307\u4ee4\u91cd\u6392\u540e\uff0c\u7ebf\u7a0b A \u5c06 instance \u8d4b\u503c\uff0c\u4f46\u8fd8\u672a\u6267\u884c\u5b9e\u4f8b\u5bf9\u8c61\u521d\u59cb\u5316\uff0c\u7ebf\u7a0b B \u5728\u5916\u5c42\u76f4\u63a5\u8ba4\u5b9a\u4e3a\u975e null\uff0c\u8fd4\u56de\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u5b8c\u6210\u7684 instance                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265663\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fengpan567 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u521d\u59cb\u5316\u7684\u65f6\u5019\u662f\u5148\u7ed9\u5bf9\u8c61\u5206\u914d\u5185\u5b58\u5730\u5740\uff0c\u4f46\u662f\u5bf9\u8c61\u8fd8\u672a\u5b9e\u9645\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u6240\u4ee5\u53ef\u80fd\u5bfc\u81f4\u5176\u4ed6\u7ebf\u7a0b\u8bef\u5224                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265664\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kkkkkrua <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             DLC \u6ca1\u90a3\u4e48\u590d\u6742\uff0c\u5176\u5b9e\u4f60\u56f0\u6270\u7684\u5730\u65b9\u6070\u5de7\u4e0d\u5728 synchronized \u8fd9\u91cc\uff0c<br \/>\u5047\u5982\u6709 A,B \u4e24\u4e2a\u7ebf\u7a0b<br \/>if (uniqueInstance == null) {<br \/>\/\/\u8fd9\u91cc\u6ca1\u6709\u9501\uff0cAB \u7ebf\u7a0b\u540c\u65f6\u8fdb\u6765\uff0c\u8fd9\u4e2a\u662f\u53ef\u80fd\u5b58\u5728\u7684<br \/> synchronized (Singleton.class) {<br \/>\/\/\u6709\u9501\u540e\uff0c\u6392\u961f\u8fdb\u5165\uff0c\u5047\u5982 A \u5148\u8fdb\u6765\uff0c\u80af\u5b9a\u662f null\uff0c\u90a3\u4e48 B \u7ebf\u7a0b\u80af\u5b9a\u8fdb\u4e0d\u53bb<br \/> if (uniqueInstance == null) {<br \/>\/\/A \u7ebf\u7a0b\u5b9e\u4f8b\u5316<br \/> uniqueInstance = new Singleton();<br \/> }<br \/> }<br \/> }<br \/>\/\/B \u8d70\u5230\u8fd9\uff0c\u8fd4\u56de\u521b\u5efa\u597d\u7684\u5b9e\u4f8b<br \/>return uniqueInstance;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265665\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : levizheng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @aerzha <br \/>@fengpan567 <br \/>@kkkkkrua <br \/>\u5176\u5b9e\u6211\u60f3\u95ee\u7684\u95ee\u9898\u5c31\u662f\uff0c\u4e3a\u4ec0\u4e48\u9664\u4e86\u4e00\u5f00\u59cb\u6301\u6709\u9501\u7684 A \u7ebf\u7a0b\u5916\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4f1a\u5728\u5916\u5c42\u5224\u65ad\u662f\u975e null \u3002<br \/>\u6211\u4e00\u76f4\u89c9\u5f97\u662f\u5f53\u7ebf\u7a0b A \u91ca\u653e\u4e86\u9501\u4e4b\u524d \u624d\u4f1a\u628a\u5de5\u4f5c\u5185\u5b58\u7684\u503c\u66f4\u65b0\u5230\u4e3b\u5185\u5b58\uff0c\u89c9\u5f97\u7ebf\u7a0b A \u5728\u6ca1\u91ca\u653e\u524d\uff0c\u5916\u9762\u7684\u6240\u6709\u7ebf\u7a0b\u90fd\u5e94\u8be5\u83b7\u53d6\u7684\u662f null \u3002\u3002\u4e0d\u77e5\u9053\u6211\u8fd9\u4e2a\u7406\u89e3\u662f\u4e0d\u662f\u9519\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265666\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : letianqiu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u8fdb\u5165\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u7136\u540e\u53d1\u73b0\u662f null\uff0c\u4f46\u662f\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u4f1a\u62ff\u5230\u9501\u7136\u540e\u521b\u5efa\u5bf9\u8c61                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265667\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : letianqiu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u60f3\u6211\u660e\u767d\u4e3b\u7684\u95ee\u9898\u4e86\u3002\u4f60\u7684\u7406\u89e3\u6709\u504f\u5dee\u3002\u56e0\u4e3a\u8fd9\u91cc\u5224\u65ad\u662f\u4e0d\u662f null \u7684 read \u4e0d\u6d89\u53ca monitor\uff0c\u6240\u4ee5\u548c synchronized \u7684 block \u91cc\u7684 write \u4e0d\u6784\u6210 happens before \u7684\u5173\u7cfb\u3002\u52a0\u4e86 volatile \u4e4b\u540e\uff0chappens before \u5c31\u6210\u7acb\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265668\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Joker123456789 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9996\u5148\u5462\uff0c\u540c\u6b65\u4e0d\u662f\u4e3a\u4e86\u9632\u6b62\u83b7\u53d6 null \u503c\uff0c\u4f60\u90fd if \uff08 xxx=null \uff09\u4e86\uff0c\u8fd8\u600e\u4e48\u53ef\u80fd\u8fd4\u56de null \u5462\uff1f \u540c\u6b65\u662f\u4e3a\u4e86\u9632\u6b62\u83b7\u53d6\u591a\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<p>\u6bd4\u5982\u9879\u76ee\u521a\u542f\u52a8\uff0c\u8fd9\u4e2a\u5355\u4f8b\u8fd8\u6ca1\u88ab\u5b9e\u4f8b\u5316\uff0c\u6b64\u65f6\u4e24\u4e2a\u5e76\u53d1\u8fc7\u6765\u4e86\u3002<\/p>\n<p>\u6bd4\u5982 A \u548c B \u540c\u65f6\u8c03\u7528\u4e86 getXX \u65b9\u6cd5\uff0c \u5f53 A \u8fdb\u5165\u4e86 if \u4ee5\u540e\uff0c\u5728\u6267\u884c xxx = new XXX(); \u4e4b\u524d\uff0cB \u8fdb\u6765\u4e86\uff0c\u6b64\u65f6 xxx \u8fd8\u662f\u7a7a\u7684\u5427\uff1f<\/p>\n<p>\u90a3\u4e48 B \u4e5f\u4f1a\u8fdb\u5165 if \u5bf9\u4e0d\u5bf9\uff1f<\/p>\n<p>\u6b64\u65f6 A \u6267\u884c\u4e86 xxx = new XXX(); \u5e76\u8fd4\u56de\u4e86\uff0cB \u624d\u5f00\u59cb\u6267\u884c xxx = new XXX(); <\/p>\n<p>A \u548c B \u662f\u4e0d\u662f\u83b7\u53d6\u5230\u7684\u5bf9\u8c61\u4e0d\u4e00\u6837\uff1f \u90a3\u8fd9\u5c31\u4e0d\u662f\u5355\u4f8b\u4e86\u554a\u3002<\/p>\n<p>\u6240\u4ee5\u5fc5\u987b\u7b49 A \u62ff\u5230\u4e86\u8fd4\u56de\u503c\uff0cB \u624d\u80fd\u8fdb\u6765\uff0c\u6240\u4ee5\u624d\u7528\u540c\u6b65\u9501\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>\u7136\u540e\u5c31\u662f\u4f60\u7684\u95ee\u9898\u4e86\uff0c\u5728\u9501\u91ca\u653e\u4e4b\u524d\uff0c\u5176\u4ed6\u7ebf\u7a0b\u662f\u65e0\u6cd5\u6267\u884c\u8fd9\u4e00\u6bb5\u4ee3\u7801\u7684\uff0c\u8fd9\u624d\u662f\u5b83\u7684\u610f\u4e49\u3002 \u81f3\u4e8e\u4f60\u8bf4\u7684\u91ca\u653e\u524d\u4f1a\u4e0d\u4f1a\u540c\u6b65\u5230\u4e3b\u5185\u5b58\uff0c\u90a3\u80af\u5b9a\u662f\u4e0d\u4f1a\u7684\uff0c\u4f46\u662f\u5982\u679c\u4f60\u52a0\u4e86 volatile \u5c31\u4f1a\u540c\u6b65\u3002<\/p>\n<p>\u6700\u540e\uff0c\u4f60\u8fd9\u6bb5\u4ee3\u7801\u6709\u70b9\u8fc7\u4e8e\u590d\u6742\u4e86\uff0c\u76f4\u63a5\u5728 getUniqueInstance \u65b9\u6cd5\u4e0a\u52a0\u4e2a\u9501\u4e0d\u5c31\u597d\u4e86\u3002\u5176\u4ed6\u7684\u90fd\u53ef\u4ee5\u5220\u6389\u4e86\u3002<br \/>\u6216\u8005\u4f60\u5e72\u8106 \u7528\u9971\u6c49\u6a21\u5f0f\uff0cprivate static Singleton uniqueInstance = new Singleton(); \u5728 getUniqueInstance \u65b9\u6cd5\u91cc\u76f4\u63a5 return \u5c31\u597d\u4e86\uff0c\u90fd\u4e0d\u9700\u8981\u9501\u3002\u8fd9\u79cd\u652f\u6301\u5e76\u53d1\uff0c\u6548\u7387\u9ad8\u4e00\u4e9b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265669\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fengpan567 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @levizheng volatile \u5173\u952e\u5b57\u662f\u5b9e\u65f6\u66f4\u65b0\u5230\u4e3b\u5185\u5b58\u7684\uff0c\u4e0d\u7528\u7b49\u5f85\u91ca\u653e\u9501                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265670\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azygote <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             volatile \u7684\u4f5c\u7528\u4e3b\u8981\u662f\u4e3a\u4e86 synchronized \u5916\u9762\u90a3\u4e2a if \u7684\u3002\u5982\u679c\u4e0d\u52a0 volatile \u7684\u8bdd\uff0c\u4f1a\u51fa\u73b0\u4e00\u4e2a\u60c5\u51b5\uff1a\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5728\u5bf9 uniqueInstance \u8d4b\u503c\uff0c\u7531\u4e8e\u6ca1\u6709 volatile \u5173\u952e\u5b57\uff0cuniqueInstance \u53ef\u80fd\u8fd4\u56de\u4e00\u4e2a\u521d\u59cb\u5316\u4e00\u534a\u4e86\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u8fd9\u65f6\u6070\u597d\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u597d\u6267\u884c\u5230 if (uniqueInstance == null)\uff0c\u5f97\u5230\u7ed3\u679c\u4e3a false \u7136\u540e\u76f4\u63a5\u8fd4\u56de, \u4f46\u5b9e\u9645\u4e0a\u8fd9\u4e2a uniqueInstance \u8bfb\u5230\u7684\u503c\u5e76\u6ca1\u6709\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u5bfc\u81f4\u8fd4\u56de\u4e86\u521d\u59cb\u5316\u4e86\u4e00\u534a\u7684\u5bf9\u8c61\uff0c\u6700\u540e\u9020\u6210\u7a0b\u5e8f crash \u3002<\/p>\n<p>\u5f15\u81ea\u7ef4\u57fa\u767e\u79d1\uff1a https:\/\/en.wikipedia.org\/wiki\/Double-checked_locking<\/p>\n<p>Intuitively, this algorithm seems like an efficient solution to the problem. However, this technique has many subtle problems and should usually be avoided. For example, consider the following sequence of events:<\/p>\n<p>Thread A notices that the value is not initialized, so it obtains the lock and begins to initialize the value.<\/p>\n<p>Due to the semantics of some programming languages, the code generated by the compiler is allowed to update the shared variable to point to a partially constructed object before A has finished performing the initialization. For example, in Java if a call to a constructor has been inlined then the shared variable may immediately be updated once the storage has been allocated but before the inlined constructor initializes the object.[6]<\/p>\n<p>Thread B notices that the shared variable has been initialized (or so it appears), and returns its value. Because thread B believes the value is already initialized, it does not acquire the lock. If B uses the object before all of the initialization done by A is seen by B (either because A has not finished initializing it or because some of the initialized values in the object have not yet percolated to the memory B uses (cache coherence)), the program will likely crash.                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265671\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : levizheng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Joker123456789 <br \/>\u61d2\u6c49\u6a21\u5f0f+\u540c\u6b65\u5173\u952e\u5b57\u6548\u7387\u8fd8\u662f\u4f4e\u7684\uff0c\u5373\u4f7f\u5df2\u7ecf\u5b9e\u4f8b\u5316\u5176\u4ed6\u7ebf\u7a0b\u8fd8\u662f\u5728\u8fdb\u884c\u7b49\u5f85\u7684<br \/>\u4f60\u8bf4\u7684\u9971\u6c49\u6a21\u5f0f\u5e94\u8be5\u662f\u997f\u6c49\u6a21\u5f0f\u628a\u3002\u8fd9\u4e2a\u6ca1\u6709\u5b9e\u73b0\u5ef6\u8fdf\u5b9e\u4f8b\u5316\uff0c\u5982\u679c\u5f88\u591a\u7684\u7c7b\u7684\u8bdd\uff0c\u8d44\u6e90\u5360\u7528\u4e5f\u662f\u6709\u7684<\/p>\n<p>\u6211\u77e5\u9053 volatile \u5173\u952e\u5b57\u662f\u4e3a\u4e86\u9632\u6b62\u6307\u4ee4\u91cd\u6392\uff0c\u4f46\u662f\u6211\u89c9\u5f97\u4f60\u8bf4\u7684\u5185\u5bb9\u4e0d\u5bf9\u54c8\u54c8\u54c8<br \/>\u4f60\u8bf4\u7684 A \u548c B \u90fd\u8fdb\u5165\u4e86 if \u91cc \u4fe9\u90fd\u662f\u7a7a \u90fd\u4f1a\u8fdb\u5165 if \u6211\u662f\u8ba4\u540c\u7684\uff0c<br \/>\u6240\u4ee5\u8fd9\u8fb9\u8981\u5728\u540c\u6b65\u4ee3\u7801\u5757\u91cc\u9762\u518d\u6b21\u5224\u65ad\u662f\u5426\u4e3a null\uff0c\u53ea\u6709\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u624d\u4f1a\u8fdb\u884c\u5b9e\u4f8b\uff0c\u5f53\u91ca\u653e\u9501\u7684\u65f6\u5019\uff0c\u5176\u4ed6\u7684\u7ebf\u7a0b\u8bfb\u5230\u91cc\u9762\u7684\u503c\u53d1\u73b0\u5df2\u7ecf\u5b9e\u4f8b\u5316\u5c31\u4e0d\u4f1a\u6267\u884c if \u91cc\u7684\u5185\u5bb9\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265672\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jooooooooo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bfb\u4e0b\u8fd9\u4e2a www.cs.umd.edu\/~pugh\/java\/memoryModel\/DoubleCheckedLocking.html                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265673\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : levizheng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @azygote \u6211\u660e\u767d\u4f60\u7684\u610f\u601d\uff0c\u6211\u7ea0\u7ed3\u7684\u662f\u5982\u679c\u6ca1\u6709 volatile \u5173\u952e\u5b57\uff0c\u5f53\u6211\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\u5728\u5b9e\u4f8b\u5316\u4e00\u534a\u7684\u65f6\u5019\uff0c\u8fd9\u65f6\u5019\u5176\u4ed6\u7ebf\u7a0b\u5728\u5916\u9762\u90a3\u4e2a if \u5224\u65ad\u8bfb\u5230\u7684\u4e0d\u5e94\u8be5\u662f\u6052\u4e3a null \u5417 \u5e94\u8be5\u4e00\u76f4\u662f true \u628a\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265674\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jooooooooo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u53e5 &#8220;\u96be\u9053\u8bf4 synchronized \u5728\u91ca\u653e\u9501\u4e4b\u524d\u5c31\u5df2\u7ecf\u4f1a\u628a\u5de5\u4f5c\u5185\u5b58\u7684\u4e00\u4e9b\u503c\u540c\u6b65\u5230\u4e3b\u5185\u5b58\uff1f\u90a3\u540c\u6b65\u8fd8\u6709\u5565\u610f\u4e49\u5462\u3002&#8221;<\/p>\n<p>\u4f60\u5c31\u7406\u89e3\u9519\u4e86\u6574\u4e2a JMM \u662f\u5728\u5e72\u561b<\/p>\n<p>\u4e0d\u540c\u786c\u4ef6\u5bf9\u4e8e\u4e0d\u540c CPU \u7684\u5185\u5b58\u4e00\u81f4\u6027\u4fdd\u969c\u4e0d\u4e00\u6837, \u6709\u4e9b\u5f88\u5f3a, \u6709\u4e9b\u5f88\u5f31, JMM \u662f\u4e3a\u4e86\u62c9\u9f50\u4e0d\u540c\u786c\u4ef6\u8868\u73b0\u505a\u7684, \u4f7f\u7528\u6237\u4e0d\u7528\u5173\u5fc3\u5e95\u5c42\u7684\u5185\u5b58\u540c\u6b65\u7a76\u7adf\u4f7f\u600e\u4e48\u505a\u7684<\/p>\n<p>\u6bd4\u5982\u6709\u4e9b\u786c\u4ef6\u5bf9\u5185\u5b58\u4e00\u81f4\u6027\u4fdd\u969c\u5f88\u5f3a, \u4f60\u4f1a\u53d1\u751f\u751f\u6210\u7684\u6307\u4ee4\u91cc\u9762\u76f4\u63a5\u628a volatile \u7ed9\u4f18\u5316\u6389\u4e86, \u56e0\u4e3a\u786c\u4ef6\u5df2\u7ecf\u4fdd\u969c\u4e86 volatile \u7684\u8bed\u4e49, \u4e0d\u9700\u8981\u989d\u5916\u7684\u5c4f\u969c\u53bb\u4fdd\u8bc1                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265675\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Joker123456789 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @levizheng <\/p>\n<p>\u9996\u5148\u7b2c\u4e00\u6bb5\uff0c\u5355\u4f8b\u5bf9\u8c61\u4e00\u65e6\u5b9e\u4f8b\u5316\u540e\uff0c\u5c31\u4f1a\u4e00\u76f4\u5b58\u5728\uff0c\u4f60\u8bf4\u997f\u6c49\u6a21\u5f0f\u5360\u7528\u8d44\u6e90\uff0c\u5176\u5b9e\u53ea\u662f\u5728\u4e00\u5f00\u59cb\u5360\u7528\u7684\u6bd4\u61d2\u6c49\u591a\uff0c\u4f46\u662f\u968f\u7740\u9879\u76ee\u7684\u8fd0\u884c\uff0c\u6240\u6709\u61d2\u6c49\u90fd\u5c06\u88ab\u5b9e\u4f8b\u5316\uff0c\u6700\u7ec8\u5360\u7528\u7684\u8d44\u6e90\u90fd\u662f\u4e00\u6837\u7684<\/p>\n<p>volatile \u8fd9\u4e2a \u6211\u8bf4\u7684\u53ef\u80fd\u4e0d\u5bf9\u5427\uff0c\u8c22\u8c22\u4f60\u7684\u6307\u70b9\uff0c\u6211\u518d\u53bb\u6df1\u5165\u5b66\u4e00\u4e0b\u3002<\/p>\n<p>\u81f3\u4e8e\u6700\u540e\u4e00\u6bb5\uff0c\u6211\u662f\u5728\u89e3\u91ca\uff0c\u540c\u6b65\u9501\u4e0d\u662f\u4e3a\u4e86\u9632\u6b62\u83b7\u53d6 null \u503c\uff0c\u800c\u662f\u4e3a\u4e86\u9632\u6b62\u83b7\u53d6\u591a\u4e2a\u5bf9\u8c61\uff0c \u6211\u5e76\u4e0d\u662f\u6309\u7167\u4ed6\u7684\u4ee3\u7801\u6765\u8bf4\u7684\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265676\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jooooooooo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u66f4\u6781\u7aef\u7684\u4f8b\u5b50, \u4f60\u5728\u5355\u6838\u673a\u5668\u8dd1\u4efb\u4f55\u591a\u7ebf\u7a0b\u7684\u7a0b\u5e8f\u4f1a\u53d1\u73b0\u4e0d\u7528\u8fd9\u4e9b\u5173\u952e\u8bcd\u90fd\u4e0d\u4f1a\u6709\u5185\u5b58\u4e0d\u4e00\u81f4\u7684\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265677\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azygote <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @levizheng \u4ed4\u7ec6\u8bfb\u8fd9\u6bb5 &#8220;Due to the semantics of some programming languages, the code generated by the compiler is allowed to update the shared variable to point to a partially constructed object before A has finished performing the initialization. For example, in Java if a call to a constructor has been inlined then the shared variable may immediately be updated once the storage has been allocated but before the inlined constructor initializes the object.[6]&#8221;<br \/>\u5982\u679c\u4e0d\u52a0 volatile, \u6709\u4e2a\u7ebf\u7a0b\u5bf9 uniqueInstance \u521d\u59cb\u5316\u5230\u4e00\u534a\uff0c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5230 synchronized \u5916\u9762\u7684\u90a3\u4e2a if (uniqueInstance == null) \u662f\u53ef\u80fd\u8fd4\u56de false \u7684\uff0c\u4e5f\u5c31\u8bfb\u5230\u7684\u4e0d\u662f null\uff0c\u800c\u662f\u4e00\u4e2a\u521d\u59cb\u5316\u4e86\u4e00\u534a\u7684\u5bf9\u8c61\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265678\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : levizheng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jooooooooo <br \/>@azygote <br \/>\u611f\u8c22\uff0c\u53d7\u6559\u4e86\uff0c\u6211\u518d\u597d\u597d\u53bb\u9886\u4f1a\u4e00\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4265679\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : orangex <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @levizheng <br \/>@Joker123456789 <br \/>\u201c\u997f\u6c49\u5f0f\u6ca1\u6709\u5ef6\u8fdf\u5b9e\u4f8b\u5316\u201d\u4e00\u8bf4\u5b9e\u5728\u662f\u4eba\u4e91\u4ea6\u4e91\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u201c\u997f\u6c49\u5f0f\u201d\uff1a<\/p>\n<p>&#8220;`java<br \/>public class SampleClass {<br \/> private static SampleClass sInst = new SampleClass();<br \/> public static SampleClass getInst() {<br \/> return sInst;<br \/> }<br \/>}<br \/>&#8220;`<\/p>\n<p>\u5b9e\u4f8b\u5316\u53d1\u751f\u5728\u4f55\u65f6\u5462\uff1f\u4e5f\u5c31\u662f`sInst = new SampleClass();`\u5728\u4ec0\u4e48\u65f6\u5019\u6267\u884c\u7684\u5462\uff1f\u5728\u7c7b\u52a0\u8f7d(\u52a0\u8f7d.class \u8fdb\u5185\u5b58 -&gt; \u94fe\u63a5 -&gt; \u521d\u59cb\u5316)\u7684\u6700\u540e\u4e00\u6b65\u201c\u521d\u59cb\u5316\u201d\u4e2d\u6267\u884c\u3002<\/p>\n<p>\u90a3\u521d\u59cb\u5316\u7684\u53d1\u751f\u662f\u6709\u6761\u4ef6\u7684\uff1a<\/p>\n<p>1. \u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<br \/>2. \u8bbf\u95ee\u7c7b\u7684\u9759\u6001\u6210\u5458<br \/>3. \u521d\u59cb\u5316\u5b50\u7c7b<br \/>4. \u865a\u62df\u673a\u6807\u8bb0\u4e3a\u542f\u52a8\u7c7b<br \/>5. \u2026\u2026<\/p>\n<p>\u56e0\u6b64\uff0c\u5f53\u4f60\u7b2c\u4e00\u6b21\u89e6\u53d1\u4e0a\u8ff0\u6761\u4ef6\u65f6\u997f\u6c49\u5f0f\u624d\u4f1a\u5b9e\u4f8b\u5316\u3002\u800c\u5982\u679c\u6839\u636e\u201c\u5355\u4f8b Class \u5185\u4e0d\u5e94\u8be5\u62e5\u6709\u9664\u4e86 getInst \u65b9\u6cd5\u5916\u7684\u5176\u4ed6\u80fd\u88ab\u8bbf\u95ee\u7684\u9759\u6001\u6210\u5458\u201d\u7684\u7406\u5ff5\u53bb\u7ec4\u7ec7\u4ee3\u7801\u7684\u8bdd\uff0c\u997f\u6c49\u5f0f\u5728\u5ef6\u8fdf\u5b9e\u4f8b\u5316\u4e0a\u4e0e\u61d2\u6c49\u65e0\u4e8c\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u54a8\u8be2\u4e00\u4e2a\u5173\u4e8e synchroniz&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/199786"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=199786"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/199786\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=199786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=199786"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=199786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}