How does the Java virtual machine implement the “happen sooner” memory model?

The Java memory model is based on a “happen-to” relationship, which applies the rules, but also allows you to optimize the implementation of the virtual machine in terms of cache invalidity.

For example, in the following case:

// thread A
private void method() {
   //code before lock
   synchronized (lockA) {
       //code inside
   }
}

// thread B
private void method2() {
   //code before lock
   synchronized (lockA) {
       //code inside
   }
}

// thread B
private void method3() {
   //code before lock
   synchronized (lockB) {
       //code inside
   }
}

if thread A calls method()and thread B tries to get lockAinside method2(), then to synchronize with, lockAit will be necessary for thread B to observe all the changes that thread A made for all its variables before releasing its lock, even the variables that were changed in the section "Code before locking "

, method3() . .

, ? , ?

, , -?

+4
2

JVM. , , . , . , , , / .

, , , . , , - .

JVM - . , (), . , , . , , . .

. , , () . , / .

volatile , , , , . synchronized . , synchronized , , , synchronized .

, , , "", , , .

, JVM, , , , , - , . . ...

+4

, , -?

. , .

, , , , Java- , , ​​, . . , , ​​ . , .

, . ​​ , . - ​​ , .

, .

( ). . , , . CPU , , .

+1

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


All Articles