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 "
method()
lockA
method2()
, method3() . .
method3()
, ? , ?
, , -?
JVM. , , . , . , , , / .
, , , . , , - .
JVM - . , (), . , , . , , . .
. , , () . , / .
volatile , , , , . synchronized . , synchronized , , , synchronized .
volatile
synchronized
, , , "", , , .
, JVM, , , , , - , . . ...
. , .
, , , , Java- , , , . . , , . , .
, . , . - , .
, .
( ). . , , . CPU , , .
Source: https://habr.com/ru/post/1609487/More articles:std :: move and RVO optimization - c ++SKVectorMapView: unrecognized selector - skmapsShould I now pass by value? - c ++Convert text file to 2d javascript array using ajax & php - javascriptNotification, for example, whatsapp or sms app android - androidHiding the calling function in JavaScript - javascriptHTML element does not remove jquery with name containing backslash - javascriptNodejs crypto.pbkdf2 result differs from CryptoJS.PBKDF2 - javascriptencode_www_form converts space to + instead of% 20 - ruby | fooobar.comWhat is the best way to perform computationally intensive tasks in Erlang without sacrificing scalability? - erlangAll Articles