What is the area of โ€‹โ€‹memory cleared or published for different threads using volatility and synchronization?

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?
+4
source share
1 answer

There are no volume limits in memory. When you have a read or write barrier, it applies to all read / write operations in memory.

Where I saw the limitation in memory mappings. When you store a memory card, you must be careful how to make it available to other threads, as this new memory mapping may not appear in another thread, immediately resulting in a bus error (and JVM failure). This seems to be the OS, since the latest versions of Linux and Windows do not seem to have this problem.

This means that in the following code, two threads executing method (), leaving the synchronized block, will expose staticVar2 to another thread, but not staticVar1, is this correct?

statixVar1 will always be cleared if staticVar2, possibly earlier. No guarantee as to when, but the order is guaranteed.

If thread A executes the method, then later thread B executes method2 (), is the staticVar2 value published from A to B, although both threads are not synchronized on the same lock?

Yes, the lock used does not matter for the guarantee - before.

Does volatile read-write clear all memory from all threads, or just between two access threads? Whatever the answer, is all memory flushed or only mutable variables?

All dirty memory is flushed to the write barrier, and all reads will be consistent in order on the read barrier. volatile writes as a write barrier to the read and write barrier.

Did all the memory with the changed memory change when exiting the synchronized block, or just the memory that was changed inside the block?

All memory modified by this thread.

Is it necessary to synchronize all the static variables accessed by two threads?

Only if one thread modifies the variable. Any number of threads can read static values โ€‹โ€‹without synchronization.

+3
source

Source: https://habr.com/ru/post/1485537/


All Articles