Sunday, 11 February 2018

10 points about volatile modifier or field in Java



The volatile modifier has always been an interesting and tricky topic to many Java programmers. I still feel that it's one of the most underutilized modifiers in Java, which can do a lot of good if understood and applied correctly, after all, it provides a lock-free way to achieve synchronization in Java.

                    If a field is shared between multiple threads and one of them change its value i.e. one thread reads from the field which is written by other threads, then, by using a volatile modifier, you can synchronize access to this field. The volatile modifier in Java provides visibility and ordering guarantee without any locking. You might know that compiler and JVM can re-order your code due to various reasons e.g. performance improvement which can be a problem in concurrent Java application.

By making a filed volatile in Java you can instruct compiler, JVM, and JIT to doesn't reorder them preventing subtle and hard-to-find multi-threading bugs.

Similarly, the visibility guarantee ensures that memory barriers are refreshed when a volatile variable is read, hence all the changes made by one thread before writing into a volatile variable is visible to the other thread who read from a volatile variable, this is also a part of "happens-before" guarantee provided by volatile variables.

Though, you need to be a little bit careful because volatile doesn't provide atomicity and mutual exclusive access, which is one of the key difference between synchronized and volatile keyword in Java. Hence, the volatile variable should only be used if the assignment is the only operation performed on them.

In this article, I am going to 10 such important points about volatile modifier in a field which will help you to learn this useful concept better in Java multi-threading world. If you want to learn more, you can always read Java concurrency in Practice by Infocampus


10 Points about volatile variable in Java

Here are a couple of useful points a Java developer should know about volatile modifier. Since many of you might not have the first-hand experience of using volatile variable in your application, this points will help you to understand the existing code written by some experienced programmers and which uses the volatile modifier for synchronization and inter-thread communication.

1) The volatile modifier provides lock-free synchronization of fields. Unlike synchronized keyword, when a thread read from the volatile variable or write into the volatile field, no lock is acquired or released.

2) The volatile modifier can only be applied to a field. You cannot apply volatile keyword with methods and local variables. Of course, you don't need any synchronization for local variables because they are not shared between multiple threads. Every thread has their own copy of local variables.

3) When you make a field volatile in Java, it signals compiler and Java virtual machine that this field may be concurrently updated by other threads. Due to this reason, compiler stops re-ordering instructions for maximum throughput involving the volatile field.
4) Apart from ordering, volatile modifier also provides visibility guarantee. Any change made to the volatile variable is visible to all threads. The value of the volatile variable is not cached by threads, instead, they are alway read from the main memory.


5) The volatile modifier also provides the happens-before guarantee, A write to volatile variable happens before any subsequent read. It also causes memory barrier to be flushed, which means all changes made by thread A before writing into the volatile variable will be visible to thread B when it read the value of the volatile field.

6) The volatile modifier provides a low-cost synchronization alternative of synchronized keyword, albeit it doesn't replace synchronized block or synchronized method because it doesn't provide atomicity or mutual exclusion. The low cost is because it's lock free. Threads are not blocked for lock and they don't spend time on acquiring and releasing the lock.


7) When to use the volatile variable is the most common question from many Java developers, even experienced developers ask this question. The reason being is the lack of opportunity to write concurrent code which makes use of the volatile variable. Well, one of the most common uses of the volatile variable is shared boolean flag. It is also one of the most popular Java concurrency interview questions for senior developers

8) Always remember, volatile fields do not provide any atomicity guarantee. For example, you cannot make a counter volatile and assume that i++ will be atomic. Similarly, flipping a volatile boolean variable is not atomic

9) You should only make a variable volatile if you perform an atomic operation on them e.g. assignment. Assigning a value to a boolean or int variables are atomic but reading and writing long and double is not atomic unless they are volatile. So, one more use of volatile keyword is to make the long and double assignment atomic in Java.


10) The key difference between volatile and synchronized modifier is a locking, the synchronized keyword need a lock but volatile is lock free. Due to this reason, the cost of synchronization is also less in the case of the volatile variable.

No comments:

Post a Comment