Java- volatile modifier / Chapter -10 / Access Modifier




Java Volatile modifier


1." volatile" is a modifier applicable only for variable and we can't apply for methods & classes.

2.If a value of a variable keeps on changing by multiple threads then there may be a chance of data inconsistency problem.We can solve this problem by using "volatile modifier".

3.If a variable declared as volatile then for every thread JVM will create a separate local copy.





4.Every modification performed by the thread will take place in the local copy so that there are no effects on remaining threads.

5.The main advantage of volatile keyword is we can overcome data inconsistency problem but the main disadvantage of volatile keyword is creating & maintaining a separate copy of every thread increases the complexity of programming & create performance problems.Hence If there is no specific requirement it is never recommended to use volatile keyword.

6.final variable means the value never changes whereas volatile variable means the value keep on changing, the hence volatile final is an illegal combination for variables.

example:-
volatile final int x=10;  Invalid

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com