Wednesday, September 14, 2016

Synchronization

      1. Important in case of multi-threaded environment
      2. Synchronization can be of Java Object or Java Class
      3. Synchronized and volatile keywords are used for synchronizing i.e. mutual exclusive access
      4. Concurrent access of shared objects introduces errors like thread interference and memory consistency. To avoid these errors object must be synchronized
      5. If shared object is read-only or immutable like ‘String’ then synchronization not required despite running multiple threads
      6. JVM guarantees java synchronized code is executed one at a time
      7. synchronized keyword is related to locking and unlocking. When entering the synchronized block or method it needs to acquire lock so it reads from main memory rather than cache memory. When exiting the synchronized block or method it needs to release the lock so it flushes write operation in main memory. This eliminates memory consistency errors.
      8. Any code with synchronized method or synchronized block is mutually exclusive
      9. static synchronized method and non-static synchronized method. In Static synchronized method class object is locked (class level lock) where as in non-static synchronized current object (this) is locked (object level lock). If we have a non-static and static method on same object/ scenario, they can run in parallel since one will run on class object and other on this object.
      public class Counter{ 
      private static int count = 0; 
      public static synchronized int getCount(){ return count;}
                  public synchoronized setCount(int count){ this.count = count; } }

      1. can’t have synchronized variable it gives compilation error. So we can have volatile variable since this reads variable from main memory rather than cache
      2. block synchronization preferred over method since only the required part is made mutually exclusive since it comes at the cost of performance
      3. if object used inside synchronized block is null then nullpointer exception is thrown
      4. java synchronized keyword is re-entrant in nature
      5. does not allow concurrent read. By using concept of lock stripping and using different locks for read and write we can overcome this limitation.
      6. synchronized keyword is used to limited access in only 1 JVM. If there are different JVM or different file bases or DB, synchronized keyword does not work and we need to implement global locks.
      7. Can result in dead-lock or starvation in multi threading is synchronization not properly implemented.
      8. Constructor can’t be synchronized
      9.  Important methods related to synchronization: wait(), notify(), notifyAll() which is defined in ObjectClass
      10. Don’t synchronize non-final fields in synchronized block since the value can change at any time and then different threads may be synchronizing different objects
      11. String is not recommended as a lock in java synchronized block because a string is an immutable object and literal string and interned string gets stored in String pool. so by any chance if any other part of the code or any third party library used same String as there lock then they both will be locked on the same object despite being completely unrelated which could result in unexpected behavior and bad performance. Instead of String object its advised to use new Object() for Synchronization in Java on synchronized block.
      private static final String LOCK = "lock";   //not recommended
         private static final Object OBJ_LOCK = new Object(); //better
         public void process() {
                                        synchronized(LOCK) {
                                                       .......
            }
         }
      1. Calendar and SimpleDateFormat classes are not thread-safe and require external synchronization in multi-threaded environment
      2.  
                     private static final String LOCK = "lock";   //not recommended
                     private static final Object OBJ_LOCK = new Object(); //better
                     public void process() {
                                     synchronized(LOCK) {
                                                    .......
                        }
                     }

No comments:

Post a Comment