简体   繁体   English

同步数据读/写到主存储器

[英]Synchronized data read/write to/from main memory

When a synchronized method is completed, will it push only the data modified by it to main memory, or all the member variables, similarly when a synchronized method executes, will it read only the data it needs from main memory or will it clear all the member variables in the cache and read their values from main memory ? 当同步方法完成时,它将仅将其修改后的数据推入主存储器或所有成员变量,类似地,当同步方法执行时,它将仅从主存储器读取所需的数据,还是清除所有缓存中的成员变量,并从主内存中读取其值? For example 例如

public class SharedData
{

    int a; int b; int c; int d;

    public SharedData()
    {
        a = b = c = d = 10;
    }

    public synchronized void compute()
    {
        a = b * 20;
        b = a + 10;
    }

    public synchronized int getResult()
    {
        return b*c;
    }

}

In the above code assume compute is executed by threadA and getResult is executed by threadB. 在上面的代码中,假定计算是由线程A执行的,而getResult是由线程B执行的。 After the execution of compute, will threadA update main memory with a and b or will it update a,b,c and d. 执行完计算后,线程A将使用a和b更新主内存,或者将更新a,b,c和d。 And before executing getResult will threadB get only the value of b and c from main memory or will it clear the cache and fetch values for all member variables a,b,c and d ? 并且在执行getResult之前,线程B将仅从主内存中获取b和c的值,还是将清除缓存并获取所有成员变量a,b,c和d的值?

synchronized ensures you have a consistent view of the data. synchronized可确保您拥有一致的数据视图。 This means you will read the latest value and other caches will get the latest value. 这意味着您将读取最新值,而其他缓存将获得最新值。 Caches are smart enough to talk to each other via a special bus (not something required by the JLS, but allowed) This bus means that it doesn't have to touch main memory to get a consistent view. 高速缓存足够智能,可以通过特殊总线相互通信(这不是JLS所必需的,但允许)。该总线意味着不必触摸主内存即可获得一致的视图。

I think following thread should answer your question. 我认为以下主题应该可以回答您的问题。

Memory effects of synchronization in Java Java同步的内存影响

In practice, the whole cache is not flushed. 实际上,不会刷新整个缓存。

1. synchronized keyword on a method or on an atomic statement, will lock the access to the resource that it can modify, by allowing only one thread to gain the lock. 1. synchronized关键字上的方法或在原子声明,将只允许一个线程获得锁锁定对资源的访问,它可以修改,。

2. Now preventing of caching of values into the variables is done by volatile keyword. 2.现在,通过 volatile关键字可以防止将值缓存到变量中 Using volatile keyword will ask the JVM to make the thread that access the instance variable to reconcile its copy of the instance variable with the one saved in the memory. 使用volatile关键字将要求JVM使访问实例变量的线程将其实例变量的副本与保存在内存中的那个变量进行协调。

3. Moreover in your above example, if threadA execute the compute() , then threadB canNot access the getResult() method simultaneously , as they both are synchronized methods, and only one thread can have access to the all the synchronized methods of the object , cause its not the method that is locked but the Object. Its 3.此外,在上面的示例中,如果threadA执行execute compute() ,则threadB 不能 同时访问getResult()方法,因为它们都是同步方法,并且只有一个线程可以访问该对象的所有同步方法。 ,导致its not the method that is locked but the Object. Its its not the method that is locked but the Object. Its like this... Every object has one lock, and the thread which wants to access its synchronized block must get that lock its not the method that is locked but the Object. Its like this... Every object has one lock, and the thread which wants to access its synchronized block must get that lock

4. Even every class has a lock , that is used to protect the crucial state of the static variables in the class. 4.甚至每个类都有一个锁 ,用于保护类中静态变量关键状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM