Volatile variables provide the guarantee about ordering and visibility e.g. Which means, if you write to volatile field then it's guaranteed that any thread accessing that variable will see the value you wrote and anything you did before doing that right into the thread is guaranteed to have happened and any updated data values will also be visible to all threads, because the memory barrier flushed all other writes to the cache.Ĥ) What guarantee volatile variable provides? ( answer) Basically, Java Memory model inserts a write barrier after you write to a volatile variable and a read barrier before you read it. Another use of the volatile variable is to provide a memory barrier, just like it is used in Disrupter framework. Both double and long are 64-bit wide and they are read in two parts, first 32-bit first time and next 32-bit second time, which is non-atomic but volatile double and long read is atomic in Java. One of the practical use of the volatile variable is to make reading double and long atomic. While reading/writing a volatile long or double (64 bit) is atomic.ģ) What are practical uses of volatile modifier? Why? because reading to a long variable is not atomic in Java and done in two steps, If one thread is writing or updating long value, it's possible for another thread to see half value (fist 32-bit). a counter, a price field or anything, you better make it volatile. If you know that a long field is accessed by more than one thread e.g. One example I have seen is having a long field in your class. This question is also not easy to answer because volatile is not about atomicity, but there are cases where you can use a volatile variable to make the operation atomic. This another good question I love to ask on volatile, mostly as a follow-up of the previous question. What I mean, if one thread changes the reference variable to points to another array, that will provide a volatile guarantee, but if multiple threads are changing individual array elements they won't be having happens before guarantee provided by the volatile modifier.Ģ) Can volatile make a non-atomic operation to atomic? Yes, you can make an array volatile in Java but only the reference which is pointing to an array, not the whole array. This is one of the tricky Java multi-threading questions you will see in senior Java developer Interview. Multithreading, Concurrency and Thread basics Questions I am sure you have seen many of these questions personally on your interviews and many of you would have answered them correctly as well. So now the time has come to introduce you to this MEGA list of 120 Java questions collected from various interviews of last 5 years. It will take you to some of the most important topics for Java and JEE interviews, worth reading even if you are not preparing for a Java interview. You guys are also lucky that nowadays there are some good books available to prepare for Java interviews, one of them which I particularly find useful and interesting to read is Java Programming Interview Exposed by Markham.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |