Visibility, atoms and orderliness of the Java memory model (rpm)

1. Visibility:

  Visibility is a complex property because the visibility of error will always contrary to our intuition. In general, we can not guarantee thread can perform a read operation in a timely manner to see the value of other threads written, and sometimes simply impossible. In order to ensure visibility among multiple threads of memory write operations, you must use the synchronization mechanism.

  Visibility means visibility between threads, a thread modifies a state of another thread is visible. That is the result of a modification of the thread. Another thread can immediately see. For example: using volatile variables would have visibility. volatile variables are not allowed inside thread and reorder buffer that directly modify the memory. So the other threads are visible. But note here a problem, volatile only let him be content with a modified visibility, but can not guarantee that it is atomic. For example, volatile int a = 0; a ++ after a operation; this variable has a visibility, but still a ++ is a non-atomic operation, that is, this operation also exists thread-safety issues.

  In Java volatile, synchronized and the final realization of visibility.

2. Atomicity:

  Atom is the smallest unit in the world, has indivisibility. For example a = 0; (a non-long and double) This operation is indivisible, then we say that this operation atomic operation. Another example: a ++; this operation is actually a = a + 1; is divisible, so he is not an atomic operation. Non-thread-safe atomic operations there will be problems, we need to use synchronous technology (sychronized) to make it into an atomic operation. Operation is atomic, then we call it atomic. provides some of the atoms under the category of concurrent java package, we can understand the usage of these atoms class by reading the API. For example: AtomicInteger, AtomicLong, AtomicReference and so on.

  In Java and synchronized in lock, unlock operation guarantee atomicity.

3. ordering:

  Java language provides two keywords volatile and synchronized to ensure orderly, volatile because of its own with "prohibit instruction reordering" of semantics, synchronized operation between threads is by "allowing only one variable at a time in the same thread lock its operation "rule obtained, the rules determine the synchronization holding two blocks of the same object lock can only be executed serially.

The following is an excerpt from "Java Concurrency in Practice":

  The following piece of code in a multithreaded environment, there will be problems.

public class NoVisibility {
    private static boolean ready;
    private static int number;
    private static class ReaderThread extends Thread {
        @Override
        public void run() {
            while(!ready) {
                Thread.yield();
            }
            System.out.println(number);
        }
    }
    public static void main(String[] args) {
        new ReaderThread().start();
        number = 42;
        ready = true;
    }
}

     NoVisibility likely to continue the cycle continues, because the reader thread might never see the value less than ready. Even NoVisibility may output 0, because the reader thread could see the value of the write ready, but no number of values ​​written after seeing a phenomenon known as "re-ordering." As long as a thread can not be detected in the reordering case (even though you can clearly see the reordering of the thread in the other thread), then it can not ensure the operation of the thread will be executed in the order specified in the program. When the main thread is written first order number, and then write in the absence of synchronization ready, then the sequential read and write threads may be seeing exactly the opposite.

  In the absence of synchronization, the compiler, the processor and so may make some adjustments to the unexpected execution order of runtime operations. In a multithreaded program lacks sufficient synchronized, in order to perform memory operations chunxu judge, can not get the right conclusion.

  The design looks like a failure, but JVM make full use of modern multi-core processors powerful performance. For example, in the absence of synchronization, Java memory model allows the compiler to reorder the sequence of operations, and the value in the register cache. In addition, it allows the CPU to reorder the sequence of operations, the processor and the cache values ​​specific cache.

Original: https://www.cnblogs.com/zhengbin/p/5654805.html

Guess you like

Origin www.cnblogs.com/laifw/p/11440066.html