This question relates only to the visibility of memory, and not to the previous and next. There are four ways in Java that ensure that changes in memory in one thread become visible to another thread. (link http://gee.cs.oswego.edu/dl/cpj/jmm.html )
- The writing stream releases the synchronization lock, and the read stream subsequently acquires the same synchronization lock.
- If a field is declared variable, any value written to it is cleared and becomes a visible write stream before the write stream performs any additional memory operation (i.e., for purposes that are immediately cleared).
- When a stream accesses the field of an object, it sees either the initial value of the field or the value recorded by some other stream.
- When the thread completes, all recorded variables are flushed to main memory.
According to Java Concurrency in practice, the bible on issues like this:
The visibility effects of variable variables extend beyond the value of the variable itself. When stream A writes to a variable, and then stream B reads the same variable, the values โโof all the variables that A were visible before writing to the variable become visible to B after reading the variable.
Flying question
Does this mean that the JVM actually tracks the variability of the variables in the volatile variable and writes them in order to know how to clear the memory from A to B, and not from A to C? So A writes to the variable, then C reads from the variable, and then B reads from the variable, flushing is performed downstream between A and B and A and C, but not B and C? Or does this mean that all cached memory is cleared, regardless of threads? Are only volatile variables stored, or all cached memories?
Synchronized Question
To clear the synchronized keyword, he says that only memory updated inside the lock is guaranteed to be published to other threads. This means that in the following code, two threads executing method() , leaving a synchronized block, will be dumped staticVar2 into another thread, but not staticVar1 , is this correct?
Also, in method2() synchronization over differentLock can lead to what happens - before that - after problems if another thread executes method() . However, the question is visibility. If thread A executes method , then later thread B executes method2() , is the value of staticVar2 published from A to B, although both threads are not synchronized on the same lock?
static int staticVar1, staticVar2; void method() { staticVar1++; synchronized (lock) { staticVar2++; } } void method2() { synchronized (differentLock) { staticVar2++; } }
Static question
It seems to me that if staticVar1 never updated to any other threads, then all static variables in any program require volatile declarations or should be available only in synchronized blocks. It seems pretty harsh, but is it true? I'm sure I saw a lot of static variables at one time that are not in sync.
Finally
- Does volatile read-write clear all memory from all threads, or just between two available threads? Whatever the answer, is all memory flushed or only mutable variables?
- Is all flushed memory reset when exiting a synchronized block or only memory that has been changed inside a block? If not all memory is blurred, does the lock object that synchronizes the stream must be the same to see the value (i.e. does the lock object have any effect on the visibility of the memory)?
- Is it necessary to synchronize all the static variables accessed by two threads?