Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java volatile loop

Tags:

java

volatile

I am working on someone's code and came across the equivalent of this:

for (int i = 0; i < someVolatileMember; i++) {
    // Removed for SO
}

Where someVolatileMember is defined like this:

private volatile int someVolatileMember;

If some thread, A, is running the for loop and another thread, B, writes to someVolatileMember then I assume the number of iterations to do would change while thread A is running the loop which is not great. I assume this would fix it:

final int someLocalVar = someVolatileMember;
for (int i = 0; i < someLocalVar; i++) {
    // Removed for SO
}

My questions are:

  • Just to confirm that the number of iterations thread A does can be changed while the for loop is active if thread B modifies someVolatileMember
  • That the local non-volatile copy is sufficient to make sure that when thread A runs the loop thread B cannot change the number of iterations
like image 884
asimes Avatar asked Oct 10 '18 15:10

asimes


People also ask

What is the volatile in Java?

For Java, “volatile” tells the compiler that the value of a variable must never be cached as its value may change outside of the scope of the program itself.

Is volatile thread safe in Java?

Unlike synchronized methods or blocks, it does not make other threads wait while one thread is working on a critical section. Therefore, the volatile keyword does not provide thread safety when non-atomic operations or composite operations are performed on shared variables.

What is difference between volatile and static?

This should read static variables are shared among objects all objects regardless of threads. "volatile variables are shared among multiple threads(so objects as well)." Volatile does not change how variables are shared among multiple threads or objects. It changes how the runtime is allowed to cache the value.

Can we have volatile methods in Java?

The volatile modifier is used to let the JVM know that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory. Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory.


1 Answers

Your understanding is correct:

  1. Per the Java Language Specification, the semantics of a volatile field ensure consistency between values seen after updates done between different threads:

    The Java programming language provides a second mechanism, volatile fields, that is more convenient than locking for some purposes.

    A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4).

    Note that even without the volatile modifier, the loop count is likely to change depending on many factors.

  2. Once a final variable is assigned, its value is never changed so the loop count will not change.

like image 71
M A Avatar answered Oct 21 '22 01:10

M A