Important points of synchronized keyword in JavaPosted by Infocampus HR on February 9th, 2018 . You can use java synchronized keyword only on synchronized method or synchronized block. 3. Whenever a thread enters into java synchronized method or blocks it acquires a lock and whenever it leaves java synchronized method or block it releases the lock. The lock is released even if thread leaves synchronized method after completion or due to any Error or Exception. 4. Java Thread acquires an object level lock when it enters into an instance synchronized java method and acquires a class level lock when it enters into static synchronized java method.
6. Java Synchronization will throw NullPointerException if object used in java synchronized block is nulle.g. synchronized (myInstance) will throw java.lang.NullPointerException if myInstance is null. 7. One Major disadvantage of Java synchronized keyword is that it doesn't allow concurrent read, which can potentially limit scalability. By using the concept of lock stripping and using different locks for reading and writing, you can overcome this limitation of synchronized in Java. You will be glad to know that java.util.concurrent.locks.ReentrantReadWriteLock provides ready-made implementation of ReadWriteLock in Java. 8. One more limitation of java synchronized keyword is that it can only be used to control access to a shared object within the same JVM. If you have more than one JVM and need to synchronize access to a shared file system or database, the Java synchronized keyword is not at all sufficient. You need to implement a kind of global lock for that. 9. Java synchronized keyword incurs a performance cost. A synchronized method in Java is very slow and can degrade performance. So use synchronization in java when it absolutely requires and consider using java synchronized block for synchronizing critical section only. 10. Java synchronized block is better than java synchronized method in Java because by using synchronized block you can only lock critical section of code and avoid locking the whole method which can possibly degrade performance. A good example of java synchronization around this concept is getting Instance() method Singleton class. See here. 11. It's possible that both static synchronized and non-static synchronized method can run simultaneously or concurrently because they lock on the different object. 12. From java 5 after a change in Java memory model reads and writes are atomic for all variables declared using the volatile keyword (including long and double variables) and simple atomic variable access is more efficient instead of accessing these variables via synchronized java code. But it requires more care and attention from the programmer to avoid memory consistency errors. 13. Java synchronized code could result in deadlock or starvation while accessing by multiple threads if synchronization is not implemented correctly. To know how to avoid deadlock in java see here. 14. According to the Java language specification you can not use Java synchronized keyword with constructor it’s illegal and result in compilation error. So you can not synchronize constructor in Java which seems logical because other threads cannot see the object being created until the thread creating it has finished it. 15. You cannot apply java synchronized keyword with variables and can not use java volatile keyword with the method. 16. Java.util.concurrent.locks extends capability provided by java synchronized keyword for writing more sophisticated programs since they offer more capabilities e.g. Reentrancy and interruptible locks. 17. Java synchronized keyword also synchronizes memory. In fact, java synchronized synchronizes the whole of thread memory with main memory. 18. Important method related to synchronization in Java are wait(), notify() and notifyAll()which is defined in Object class. Do you know, why they are defined in java.lang.object class instead of java.lang.Thread? You can find some reasons, which make sense.
private String lock = new String("lock"); synchronized(lock){ System.out.println("locking on :" + lock); } any if you write synchronized code like above in java you may get a warning "Synchronization on the non-final field" in IDE like Netbeans and InteliJ
privatestaticfinal String LOCK = "lock"; //not recommended privatestaticfinal Object OBJ_LOCK = new Object(); //better publicvoidprocess() { synchronized(LOCK) { ........ } }
Like it? Share it!More by this author |