top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are practical uses of volatile modifier in Java?

0 votes
363 views
What are practical uses of volatile modifier in Java?
posted Oct 21, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Volatile: In a multithreaded scenario when a shared variable value is modified by Thread1 might not be immediately updated to main memory. When Thread2 reads, it will get the old value. You can avoid this by specifying the variable with volatile modifier. When we specify the variable as volatile, its value will be synchronized with main copy as and when it modified. This solution is not 100 percent accurate. To get accurate results in multi threaded scenario you can use atomic classes.

Transient: This modifier is used when we don't want to serialize the value of the member variable when serializing the object.

answer Nov 1, 2016 by Shyam
...