Synchronized modifier is the modifier applicable for methods but not for classes and variables.
10 If a method or a block declared as synchronized then at a time only one Thread is allowed to operate on the given object.
2)The main advantage of synchronized modifier is we can resolve data inconsistency problems.
3) But the main disadvantage of synchronized modifier is it increases the waiting time of the Thread and effects performance of the system .Hence if there is no specific requirement it is never recommended to use.
The main purpose of this modifier is to reduce the data inconsistence problems.
1) In the above case multiple threads are accessing the same methods hence we are getting data inconsistency problems. These methods are not thread safe methods.
2) But in this case multiple threads are executing so the performance of the application will be increased.
1) In the above case only one thread is allow to operate on particular method so the data inconsistency problems will be reduced.
2) Only one thread is allowed to access so the performance of the application will be reduced.
3) If we are using above approach there is no multithreading concept.Hence it is not recommended to use the synchronized modifier in the multithreading programming.
The threads wchich are executed at background is called daemon threads.
Ex:- garbage collector,ThreadSchedular.default exceptional handler.
The threads which are executed fore ground is called non-daemon threads.
Ex:- normal java application.
Volatile modifier is also applicable only for variables but not for methods and classes.
1. If the values of a variable keep on changing such type of variables we have to declare with volatile modifier.
2.If a variable declared as a volatile then for every Thread a separate local copy will be created.
3.Every intermediate modification performed by that Thread will take place in local copy instead of master copy.
4.Once the value got finalized just before terminating the Thread the master copy value will be updated with the local stable value. The main advantage of volatile modifier is we can resolve the data inconsistency problem.
5.But the main disadvantage is creating and maintaining a separate copy for every Thread
6. Increases the complexity of the programming and effects performance of the system.