Friday, 22 April 2016

When Synchronization is not needed ?


If your code is executing in multi-threaded environment you need synchronization for objects which are shared among multiple threads to avoid any corruption of state or any kind of unexpected behavior.

Synchronization in Java will only be needed if shared object is mutable.
If your shared object is read-only or immutable object you don't need synchronization despite running multiple threads.
Same is true with what threads are doing with object if all the threads are only reading value then you don't require synchronization in java.



JVM guarantees that Java synchronized code will only be executed by one thread at a time.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.