What is volatile variable in Java - When to use
2015-03-31 15:11
190 查看
volatile variable in Java is
a special variable which is used to signal threads, compiler that this particular variables values is going to be updated by multiple thread
inside Java application. By making a variable volatile using volatilekeyword
in Java, application programmer ensures that its value should always been read from main
memory and thread should not used cached value of that variable from there own stack. With the introduction of Java memory model from Java
5 onwards along with introduction of CountDownLatch, CyclicBarrier, Semaphore and ConcurrentHashMap,
volatile variable also guarantees "happens-before" relationship,
which means not only other thread has visibility of latest value of volatile variable but also all the variable seen by the thread which has updated value of volatile variable before this threads sees it. What is volatile variable and when to use it is always
a popular
Java threading question.
Important point related to volatile keyword in Java
Since volatile keyword is used to make any variable volatile in Java environment, its good to know more about What
is volatile keyword, what is its limitation and How to use volatile keyword in Java.
![](http://2.bp.blogspot.com/-OQ_wj5FWmqY/UC-TYphU4pI/AAAAAAAAAds/jUe8AnHTiSo/s1600/50x50.gif)
1) Volatile keyword can only be applied to variable, it can not be applied to class or method. using volatile keyword along with class and method
is compiler error.
2) volatile is also refereed as modifier in Java.
When to use Volatile variable in Java
This is the most important thing to learn while learning about volatile
variable in Java. When to use volatile variable in Java is also a famous multi-threading interview question in Java. here are some of
the scenario where you can use volatile variable in Java :
1) Any variable which is shared between multiple threads
should be made variable, in order to ensure that all thread must see latest value of volatile variable.
2) A signal to compiler and JIT to ensure that compiler does not change ordering
or volatile variable and moves them out of synchronized context.
3) You want to save cost of synchronization as volatile variables are less
expensive than synchronization.
That's all on What is volatile variable in Java, When to use volatile variable in Java. volatile variable is an important concept to understand
and use. Its also very important in terms of Java interview point of view.
Other tutorial on
Java from this blog
Top
thread interview question for Java programmer - Beginners
Top
Java coding interview question for Senior Java programmer
a special variable which is used to signal threads, compiler that this particular variables values is going to be updated by multiple thread
inside Java application. By making a variable volatile using volatilekeyword
in Java, application programmer ensures that its value should always been read from main
memory and thread should not used cached value of that variable from there own stack. With the introduction of Java memory model from Java
5 onwards along with introduction of CountDownLatch, CyclicBarrier, Semaphore and ConcurrentHashMap,
volatile variable also guarantees "happens-before" relationship,
which means not only other thread has visibility of latest value of volatile variable but also all the variable seen by the thread which has updated value of volatile variable before this threads sees it. What is volatile variable and when to use it is always
a popular
Java threading question.
Important point related to volatile keyword in Java
Since volatile keyword is used to make any variable volatile in Java environment, its good to know more about What
is volatile keyword, what is its limitation and How to use volatile keyword in Java.
![](http://2.bp.blogspot.com/-OQ_wj5FWmqY/UC-TYphU4pI/AAAAAAAAAds/jUe8AnHTiSo/s1600/50x50.gif)
1) Volatile keyword can only be applied to variable, it can not be applied to class or method. using volatile keyword along with class and method
is compiler error.
2) volatile is also refereed as modifier in Java.
When to use Volatile variable in Java
This is the most important thing to learn while learning about volatile
variable in Java. When to use volatile variable in Java is also a famous multi-threading interview question in Java. here are some of
the scenario where you can use volatile variable in Java :
1) Any variable which is shared between multiple threads
should be made variable, in order to ensure that all thread must see latest value of volatile variable.
2) A signal to compiler and JIT to ensure that compiler does not change ordering
or volatile variable and moves them out of synchronized context.
3) You want to save cost of synchronization as volatile variables are less
expensive than synchronization.
That's all on What is volatile variable in Java, When to use volatile variable in Java. volatile variable is an important concept to understand
and use. Its also very important in terms of Java interview point of view.
Other tutorial on
Java from this blog
Top
thread interview question for Java programmer - Beginners
Top
Java coding interview question for Senior Java programmer
相关文章推荐
- (Page 2 of 3 )A walking tour of JavaBeans 2 :What JavaBeans is, how it works, and why you want to use it
- Node.js async in practice: When to use what?
- What is reflection and how to use it in CSharp ?
- Why and when to use AtomicIntegerFieldUpdater in java?
- Node.js async in practice: When to use what?
- When to use comparable and comparator interfaces in java
- (Page 1 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- (Page 3 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it
- What happens when more restrictive access is given to a derived class method in C++?
- Exception in thread "main" java.lang.RuntimeException: Hive metastore database is not initialized. Please use schematool (e.g. ./schematool -initSchema -dbType ...) to create the schema. If needed, do
- To see what is in the MAT-file prior to loading it, use whos -file
- Disaster Recovery: What to do when the SA account password is lost in SQL Server 2005
- IDEA 编译错误:java: try-with-resources is not supported in -source 1.6 (use -source 7 or higher to enable try-with-resources)
- What is the best way to handle Invalid CSRF token found in the request when session times out in Spring security
- When to use static method in a java class
- What is XMLHTTP? How to use security zones in Internet Explorer
- When to use static method in a java class
- What CommandName's to use in a FormView
- MySpace Unraveled: What it is and how to use it safely