What is synchronization and why it is important?

What is synchronization and why it is important?
  • Synchronized is the modifier applicable only for methods and blocks but not for classes and variables.
  • If multiple threads are trying to operate simultaneously on the same java object then there may be a chance of data-inconsistency problem.
  • To overcome this problem we should go for the synchronized keyword.
  • If a method or block declared as synchronized then at a time only one thread is allowed to execute that method or block on the given object so that data-inconsistency will be resolved.
  • The main advantage of the synchronized keyword is we can resolve data-inconsistency problems but the main disadvantage of the synchronized keyword is it increases waiting time of thread and creates performance problems, hence if there is no specific requirement then it is not recommended to use synchronized keyword.
  • The internally synchronized concept is implemented by using the lock. Every object in Java has a unique lock.
  • Whenever we are using synchronized keyword then only lock concept will come into the picture.
  • If a thread wants to execute synchronized method on the given object first it has to get Lock of that object. Once thread got the lock then it is allowed to execute any synchronized method on that object.
  • Once method execution completes automatically thread releases the lock.
  • Acquiring and releasing lock internally takes care by JVM and programmer not responsible for this activity.
  • While a thread executing synchronized method on the given object the remaining threads are not allowed to execute any synchronized method simultaneously on the same object but remaining threads allowed to execute non-synchronized method simultaneously.
  • Lock concept is implemented based on Object but not based on method. 
What is synchronization and why it is important?

What is synchronization and why it is important?

What is synchronization and why it is important? Synchronized is the modifier applicable only for methods and blocks but not for classes and variables. If multiple threads are trying to operate simultaneously on the same java object then there may be a chance of data-inconsistency problem. To overcome this problem we should go for the synchronized keyword. If a method or block declared as synchronized then at a time only one thread is allowed to execute that method or block on the given object so that data-inconsistency will be resolved. The main advantage of the synchronized keyword is we can resolve data-inconsistency problems but…

Review Overview

User Rating: Be the first one !

Leave a Reply

Your email address will not be published. Required fields are marked *

*