0% found this document useful (0 votes)
3 views3 pages

Java Constructors and Synchronization Explained

Rule 1 and Rule 3 state that the default constructor of the base class can be called from the derived class constructor using super() which is optional. Rule 2 and Rule 4 state that a parameterized constructor of the base class must be called from the derived class constructor using super(...) as the base class may have multiple parameterized constructors. The synchronized keyword is used to allow only one thread at a time to operate on a given object. It can be applied to methods or blocks but not classes or variables. Its main advantage is resolving data inconsistency problems but it increases thread waiting time and reduces performance. The volatile keyword is for variables that frequently change values. It creates a separate copy for each thread to avoid modifying the master

Uploaded by

Swetha Chowdary
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Constructors and Synchronization Explained

Rule 1 and Rule 3 state that the default constructor of the base class can be called from the derived class constructor using super() which is optional. Rule 2 and Rule 4 state that a parameterized constructor of the base class must be called from the derived class constructor using super(...) as the base class may have multiple parameterized constructors. The synchronized keyword is used to allow only one thread at a time to operate on a given object. It can be applied to methods or blocks but not classes or variables. Its main advantage is resolving data inconsistency problems but it increases thread waiting time and reduces performance. The volatile keyword is for variables that frequently change values. It creates a separate copy for each thread to avoid modifying the master

Uploaded by

Swetha Chowdary
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Rule 1 and Rule 3

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

base class constructor contains single form of default constructor?

Rule 2 and Rule 4

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

because a base class may contain multiple forms of parameterized constructors.

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

thread is allowed to operate on the given object.


Synchronized is a Modifier which is applicable for the method or block, we can not

declare classor variable with this modifier.

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

effect performance of the system, Hence if there is no specific requirement it is never

recommended to use synchronized keyword.


Volatile Keyword

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

updated with local stable value.

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

never changes. Hence final-Volatile combination is illegal combination for variables.

You might also like