Will there be an instruction / instruction that comes before the other / s should be executed first?

Consider a snippet taken from Joshua Bloch’s Java Concurrency book -

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;                            // Statement 1
        ready = true;                           // Statement 2
    }
}

For the main thread launched by the JVM, it is guaranteed that statement 1 will be executed before statement 2 .

I understand very well that ReaderThread may not see the updated value above two static variables. I do not ask permission. But if statement 1 was executed before statement 2, is it possible for ReaderThread to display the updated value ready , and not for number ? Is that what reordering means ?


-

, , , , .

-

... , ... -

- ( ) .

, , " , "? , , . ?

+4
1

. , , volatile. , .

(, .)


()

java- , jvm. ( ) , . , , .

, . , , .

y = ++x;

-

1. move from @x to register1
2. increment register1
3. move from register1 to @x
4. move from register1 to @y
5. synchronize @x and @y

-. , , .

, , , . , : 4 3 , 3 - .

3. 4. JIT, / , y. volatile.

, . , , byte 4- . , , , . , , , C . .

+2

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


All Articles