суббота, 5 сентября 2015 г.

Sublime Text regex detecting multiline tags

If you want to make a multiline search in sublime, for example for the following text:
 [sometag] here is more text  
 it spans more than one line [/sometag]  
 [sometag]this is a bit of text[/sometag]  

Use following regexp pattern:
 (?s)\[sometag\](.*?)\[\/sometag\]  
example

понедельник, 24 августа 2015 г.

DCL (Double Checked Locking) is not working in Java

It was confusing for me, but today I learned that DCL Pattern is not working in Java.
Take a look:

Double-checked locking example
 public static Singleton   
   getInstance() {    
    if (instance == null) {    
     synchronized(Singleton.class) {    
       //1    
       if (instance == null)    
        //2 instance = new Singleton();    
       //3    
     }    
    }    
   return instance;    
  }   
The theory behind double-checked locking is that the second check at //2 makes it impossible for two different Singleton objects to be created. Consider the following sequence of events:
  1. Thread 1 enters the getInstance() method.
  2. Thread 1 enters the synchronized block at //1 because instance is null.
  3. Thread 1 is preempted by thread 2.
  4. Thread 2 enters the getInstance() method.
  5. Thread 2 attempts to acquire the lock at //1 because instance is still null. However, because thread 1 holds the lock, thread 2 blocks at //1.
  6. Thread 2 is preempted by thread 1.
  7. Thread 1 executes and because instance is still null at //2, creates a Singleton object and assigns its reference to instance.
  8. Thread 1 exits the synchronized block and returns instance from the getInstance() method.
  9. Thread 1 is preempted by thread 2.
  10. Thread 2 acquires the lock at //1 and checks to see if instance is null.
  11. Because instance is non-null, a second Singleton object is not created and the one created by thread 1 is returned.
The theory behind double-checked locking is perfect. Unfortunately, reality is entirely different. The problem with double-checked locking is that there is no guarantee it will work on single or multi-processor machines.
The issue of the failure of double-checked locking is not due to implementation bugs in JVMs but to the current Java platform memory model. The memory model allows what is known as "out-of-order writes" and is a prime reason why this idiom fails. The problem with this code is that the variable instance can become non-null before the body of the Singleton constructor executes.
Source: http://www.ibm.com/developerworks/java/library/j-dcl/index.html

Instead of DCL you can use:


1. You don't need lazy initializing

 public class Singleton1 {  
         private static final Singleton1 instance = new Singleton1();  
         public static Singleton1 getInstance() {  
         return instance;  
     }  
 }  

2. You need lazy initializing and you can ignore synchronization expenses

 public class Singleton2 {  
     private static final Singleton2 instance;  
     public synchronized static Singleton2 getInstance() {  
         if (instance == null)  
             instance = new Singleton2();  
         return instance;  
     }  
 }  
In modern JVM these expenses are minimal.


3. You need lazy initializing and you can't ignore synchronization expenses 

 public class Singleton3 {  
     private static class Instance {  
         static final Singleton3 value = new Singleton3();  
     }  
     public static Singleton3 getInstance() {  
         return Instance.value;  
     }  
 }  

In that case static inner class will be loaded when getInstance() method will be called.

Hello!

Hello! My name is Boris. I'm from Saint-Petersburg, Russia. And I'm a programmer. Welcome to my blog!