Home > Java > javaTutorial > What is a memory model and why is it needed?

What is a memory model and why is it needed?

零下一度
Release: 2017-06-25 10:32:56
Original
2209 people have browsed it

JAVA Memory Model

In this series of multi-threading, we will not explore the bottom layer of the memory model

1. What is the memory model and why is it needed

In modern multi-core processors, each processor has its own cache, which is regularly coordinated with the main memory;

If you want to ensure that each processor knows what other processors are doing at any time, you will Requires a lot of overhead; and is usually unnecessary

We only need to know information when we need to share data across threads; in JAVA this is achieved through correct synchronization

1. Reordering

As follows: It will be very difficult to judge the output value

public class PossibleReordering {static int x = 0, y = 0;static int a = 0, b = 0;/** * 判断输出值将会非常困难:
     * 1:多线程之间的切换,导致可能的输出值:(0,1)(1,0)(1,1)
     * 2.指令重排序:one线程如a=1和x=b之间重排序,x=b(0),然后other线程被调度执行y=a(0),将导致(0,0)     */public static void main(String[] args) throws InterruptedException {
        Thread one = new Thread(new Runnable() {public void run() {
                a = 1;
                x = b;
            }
        });
        Thread other = new Thread(new Runnable() {public void run() {
                b = 1;
                y = a;
            }
        });
        one.start();
        other.start();
        one.join();
        other.join();
        System.out.println("( " + x + "," + y + ")");
    }
}
Copy after login

2. Introduction to JAVA memory model

Partial ordering relationship: anti-symmetric, reflexive and transitive properties; but for any two elements A and B, it does not necessarily satisfy the relationship that A favors B or B favors A

  For example: between A and B I prefer B, but I don’t need to make an explicit choice

JMM defines a partial ordering relationship for all operations in the program, called Happens-Before; if you want to ensure that the thread executing B operation, see To the result of the thread that performs operation A, regardless of whether AB is in the same thread, it must satisfy the Happens-Before relationship, otherwise the JVM will reorder it

  

For example: locking operation can predict the execution order, and multiple threads comply with Happens-Before. Without locking, it is impossible to judge the scheduling between threads,

3. Release

The real reason: There is no Happens-Before relationship between publishing a shared object and accessing the object in another thread; due to the reordering of instructions, the object is released without being constructed correctly

public class UnsafeLazyInitialization {private static Resource resource;/** * 除了竟态条件问题检查后执行,还有不安全发布的问题
     * 如:一个线程A进来,看到resource为null,则实例化并返回;另一个线程B进来看到resource不为null直接返回
     * 如果在线程A中对resource进行了修改,则可能在线程B中看不到resource的正确状态     */public static Resource getInstance() {if (resource == null)
            resource = new Resource(); // unsafe publicationreturn resource;
    }static class Resource {
    }
}
Copy after login

  

The above is the detailed content of What is a memory model and why is it needed?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template