synchronized Modifier in Java

synchronized Modifier in Java

synchronized Modifier is applicable for methods and blocks in Java. We can not use synchronized modifiers 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, this problem is known as race condition. with the help of synchronized keywords, we can solve the race problem.

synchronized Modifier Properties

  • If a method or block is declared as synchronized then at a time only one thread is allowed to execute that method or code block and the data inconsistency problem is resolved .
  • The main disadvantage of a synchronized modifier is, it increases the waiting time for threads and performance4 problems.

Hence if there is no specific requirement then it is not recommended to use a synchronized modifier.

It is compulsory that the synchronized method should contain implementation whereas the abstract method does not contain implementation. Hence abstract synchronized combination for methods is illegal.

Similar Java Tutorials

Leave a Comment