Due to thin air safety

Java Concurrency in practice explains this concept:

When a stream reads a variable without synchronization, it can see but at least it sees the value that some stream actually placed there, and not some random value. This safety guarantee is called outdoor safety.

Is this type of security weak because it may include an obsolete value?

Perhaps this snippet,, at least it sees a value that was actually placed there by some thread than some random valuewas mentioned, since the previous topic of the book was the possibility that the JVM could override variable operators with reference to sharing variables without synchronization?

Example: depending on reordering: 42 or 0 may be printed.

public class NoVisibility {
    private static boolean ready;
    private static int number;

    private static class ReaderThread extends Thread {
        public void run() {
            while(!ready)  
                Thread.yield();
            System.out.println(number);
            }
        }

    public static void main(String[] args) {
        new ReaderThread().start();
        number = 42;
        ready = true;
    }
}

EDITED - deleted comment comment.

+1
2

, ?

. "Java Concurrency in Practice" , number 0 42 , , () 1 - "--". , , long 64- , .

number 0, 42, number ReaderThread.

Edit:

Voo yshavit, JLS 17.7 , , 64- 2 32- . , . "- ", , -, , - .

+5

" " , , , " ", "". , OOTA-, , . , OOTA, .

" : " (PDF ACM.org). , , OOTA, .

, , (), , . , , , " ​​ " : A , x = 42, B 42 , B x = 42, A , y = 17... A , x = 42!

void rivera() {                 void lemon() {
    if (x == 42) {                  if (y == 17) {
        y = 17;                         x = 42;
    }                               }
}                               }

", OOTA", , ( -Python-Javascript, )

void rivera() {
    assert(y not in cache);  // for the sake of argument
    cache.y = 17;  // for the sake of speed, speculatively report that y=17
    if (x not in cache) {
        cache.x = x;  // meanwhile, slowly fetch the real value of x
    }
    if (cache.x != 42) {
        delete cache.y;  // discard the earlier, speculative write
    }
    // and then later write cache.y back into the y in main memory
}

, , lemon() rivera() cache.y = 17 . , , , x=42 y=17, !

, , , 42 17 " ", , - .;)

+2

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


All Articles