You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2019/04/22 03:46:23 UTC

[GitHub] [skywalking] IanCao edited a comment on issue #2503: avoid reordering

IanCao edited a comment on issue #2503: avoid reordering
URL: https://github.com/apache/skywalking/pull/2503#issuecomment-485316051
 
 
   ```
    public static FileWriter get() {
           if (INSTANCE == null) {
               synchronized (CREATE_LOCK) {
                   if (INSTANCE == null) {
                       INSTANCE = new FileWriter();
                   }
               }
           }
           return INSTANCE;
       }
   ```
   
   this is the FileWriter double check instantiation code.
   
   `uniqueInstance = new Singleton()` actually process in three steps
   1. allocate memory for `uniqueInstance ` reference
   2. initialize `uniqueInstance ` 
   3. make step1 point step 2
   
   but jvm has reorder characteristics, that order maybe change to 1,3,2
   for example, 
   Thread 1 process step 1 and 3
   at the same time,  Thread 2 process `getUniqueInstance()`  and result is not null, but this time `uniqueInstance` stiil not be initialized.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services