Rule 1 and Rule 3: Synchronized Keyword Is Used For When We Want To Allowed Only One Thread at A Time Then Use
Rule 1 and Rule 3: Synchronized Keyword Is Used For When We Want To Allowed Only One Thread at A Time Then Use
Whenever the derived class constructor want to call default constructor of base class, in the
context of derived class constructors we write super(). Which is optional to write because every
Whenever the derived class constructor wants to call parameterized constructor of base class in
the context of derived class constructor we must write super(...). which is mandatory to write
Synchronized Keyword
Synchronized Keyword is used for when we want to allowed only one thread at a time then use
Synchronized modifier. If a method or block declared as a Synchronized then at a time only one
Advantage of Synchronized
The main advantage of Synchronized keyword is we can resolve data inconsistency problem.
Dis-Advantage of Synchronized
The main dis-advantage of Synchronized keyword is it increased the waiting time of thread and
If the variable keep on changing such type of variables we have to declare with volatile modifier.
Volatile is a modifier applicable only for variables but not for method and class.
If a variable declared as volatile then for every thread a separate local copy will be created. Every
intermediate modification performed by that thread will takes place in local copy instead of master
copy. Once the value got finalized just before terminating the thread the master copy value will be
Advantage of Volatile
The main advantage of Volatile keyword is we can resolve data inconsistency problems.
Dis-Advantage of Volatile
The main dis-advantage of Volatile keyword is, crating and maintaining a separate copy for every
thread, increases complexity of the programming and effects performance of the system. Hence if
there is no specific requirement it is never recommended to use volatile keyword, and it is almost
outdated keyword.
Note: Volatile variable means its value keep on changing where as final variable means its value