Saturday, December 13, 2014

Java Synchronization

Synchronization is the tool using which we can achieve thread safety, JVM guarantees that synchronized code will be executed by only one thread at a time. java keyword synchronized is used to create synchronized code and internally it uses locks on Object or Class to make sure only one thread is executing the synchronized code.
·         Java synchronization works on locking and unlocking of resource, before any thread enters into synchronized code, it has to acquire lock on the Object and when code execution ends, it unlocks the resource that can be locked by other threads. In the mean time other threads are in wait state to lock the synchronized resource.
·         We can use synchronized keyword in two ways, one is to make a complete method synchronized and other way is to create synchronized block.
·         When a method is synchronized, it locks the Object, if method is static it locks the Class, so it’s always best practice to use synchronized block to lock the only sections of method that needs synchronization.
·         While creating synchronized block, we need to provide the resource on which lock will be acquired, it can be XYZ.class or any Object field of the class.
·         synchronized(this) will lock the Object before entering into the synchronized block.
·         You should use the lowest level of locking, for example if there are multiple synchronized block in a class and one of them is locking the Object, then other synchronized blocks will also be not available for execution by other threads. When we lock an Object, it acquires lock on all the fields of the Object.
·         Java Synchronization provides data integrity on the cost of performance, so it should be used only when it’s absolutely necessary.
·         Java Synchronization works only in the same JVM, so if you need to lock some resource in multiple JVM environments, it will not work and you might have to look after some global locking mechanism.
·         Java Synchronization could result in deadlocks; check this post about deadlock in java and how to avoid them.
·         Java synchronized keyword cannot be used for constructors and variables.
·         It is preferable to create a dummy private Object to use for synchronized block, so that its reference can’t be changed by any other code. For example if you have a setter method for Object on which you are synchronizing, it’s reference can be changed by some other code leads to parallel execution of the synchronized block.

No comments:

Post a Comment