使用 volatile 和 synchronized 时,内存刷新或发布到各个线程的范围是什么? [英] What is the scope of memory flushed or published to various threads when using volatile and synchronized?

查看:13
本文介绍了使用 volatile 和 synchronized 时,内存刷新或发布到各个线程的范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题仅涉及内存可见性,而不是发生在之前和之后.Java中有四种方法可以保证一个线程中对内存的更改对另一个线程可见.(参考 http://gee.cs.oswego.edu/dl/cpj/jmm.html)

This question is in reference to memory visibility only, not happens-before and happens-after. There are four ways in Java that guarantees changes to memory in one thread to be made visible to another thread. (reference http://gee.cs.oswego.edu/dl/cpj/jmm.html)

  1. 写入线程释放同步锁,读取线程随后获取相同的同步锁.
  2. 如果一个字段被声明为易失性,写入它的任何值都会在写入线程执行任何进一步的内存操作之前被写入线程刷新并使其可见(即,为了手头的目的,它会立即刷新).
  3. 线程第一次访问对象的字段时,会看到该字段的初始值或其他线程写入后的值.
  4. 当线程终止时,所有写入的变量都会刷新到主内存.

根据Java Concurrency in Practice,关于此类问题的圣经:

According to Java Concurrency in Practice, the bible on such questions:

volatile 变量的可见性影响超出了 volatile 变量本身的值.当线程 A 写入一个 volatile 变量,随后线程 B 读取同一个变量时,所有变量的值对 可见写入 volatile 变量之前的 A 在读入 volatile 变量后对 B 可见.

The visibility effects of volatile variables extend beyond the value of the volatile variable itself. When thread A writes to a volatile variable and subsequently thread B reads that same variable, the values of all variables that were visible to A prior to writing to the volatile variable become visible to B after readin the volatile variable.

不稳定的问题

这是否意味着 JVM 实际上会跟踪 volatile 变量的读取和写入,以便知道如何将内存从 A 刷新到 B 而不是 AC?所以 A 写入变量,然后 C 从变量读取,然后 B 从变量读取,刷新完成AB 以及 AC 之间的每个线程基础,但 不是 BC?或者,这是否意味着所有缓存的内存都被刷新,而不管线程如何?是只刷新 volatile 变量,还是所有缓存的内存?

Volatile question

Does this mean that the JVM actually keeps track of volatile variable reads and writes, in order to know how to flush memory from A to B and not A to C? So A writes to the variable, and later C reads from the variable, and then later B reads from the variable, the flushing is done on a per-thread basis between A and B and A and C, but not B and C? Or, does it imply that all cached memory is flushed, regardless of threads? Are only the volatile variables flushed, or all cached memory?

对于 synchronized 关键字刷新,它表示只有在锁内更新的内存才能保证发布到其他线程.这意味着在下面的代码中,两个线程运行 method(),离开同步块会将 staticVar2 刷新到另一个线程,但 不是staticVar1,对吗?

For the synchronized keyword flushing, it says that only memory updated inside the lock is guaranteed to be published to other threads. That implies that in the following code, two threads running method(), leaving the synchronized block will flush staticVar2 to the other thread, but not staticVar1, is that correct?

另外,在 method2() 中,如果另一个线程正在执行 method(),通过 differentLock 进行同步可能会导致发生前发生后发生问题>.但是,问题在于可见性.如果线程A执行method,那么后面线程B执行method2(),就是的值staticVar2A 发布到 B,即使两个线程没有在同一个锁上同步?

Also, in method2(), synchronizing over differentLock can cause happens-before happens-after problems if another thread is executing method(). However, the question is in terms of visibility. If thread A executes method, then later thread B executes method2(), is the value of staticVar2 published from A to B, even though the two threads don't synchronize over the same lock?

static int staticVar1, staticVar2;
void method() {
    staticVar1++;
    synchronized (lock) {
        staticVar2++;
    }
}
void method2() {
    synchronized (differentLock) {
        staticVar2++;
    }
}

静态问题

在我看来,如果 staticVar1 从未更新到其他线程,那么任何程序中的所有静态变量都需要 volatile 声明,或者只能在 <代码>同步块.这似乎相当苛刻,但它是正确的吗?我确实在我的时间里看到了很多不同步的静态变量.

Static question

It appears to me that if staticVar1 is never updated to other threads, then all static variables in any program require a volatile declaration, or should only be accessed in synchronized blocks. That seems rather harsh, but is it correct? I've sure seen a whole lot of static variables in my time that aren't synchronized.

  1. 易失性读写是否将所有内存刷新到所有线程,还是仅在两个访问线程之间?无论答案是什么,是所有内存都被刷新还是只有 volatile 变量?
  2. 退出同步块时是否刷新所有更改的内存,还是仅刷新块内更改的内存?如果不是所有内存都被刷新,线程同步的锁对象是否必须相同才能看到值(即锁对象对内存可见性有任何影响)?
  3. 两个线程访问的所有静态变量都必须同步吗?

推荐答案

内存方面没有范围限制.当你有一个读或写屏障时,它适用于所有内存读/写.

There is no scope limitation in terms of memory. When you have a read or write barrier it applies for all memory reads/writes.

我看到的限制是内存映射.当您对文件进行内存映射时,您必须小心如何将其提供给另一个线程,因为这个新的内存映射可能在另一个线程中不可见,这会立即导致 BUS 错误(以及 JVM 崩溃) 这似乎是OS bug 作为最新版本的 Linux 和 Windows 似乎没有这个问题.

Where I have seen a limitation is in memory mappings. When you memory map a file you have to be careful how you make this available to another threads as this new memory mapping might not be visible in another thread immediately resulting in a BUS error (and a crash of the JVM) This appears to be a OS bug as the newest versions of Linux and Windows don't appear to have this problem.

这意味着在下面的代码中,两个线程运行method(),离开同步块会将staticVar2刷新到另一个线程,但不会刷新staticVar1,对吗?

That implies that in the following code, two threads running method(), leaving the synchronized block will flush staticVar2 to the other thread, but not staticVar1, is that correct?

statixVar1 总是在 staticVar2 被刷新时被刷新,也许更快.不保证什么时候,但是保证顺序.

statixVar1 will always be flushed when staticVar2 is, perhaps sooner. No guarantee as to when, but the order is guaranteed.

如果线程 A 执行了方法,然后线程 B 执行了 method2(),那么 staticVar2 的值是否从 A 发布到 B,即使两个线程没有在同一个锁上同步?

If thread A executes method, then later thread B executes method2(), is the value of staticVar2 published from A to B, even though the two threads don't synchronize over the same lock?

是的,使用的锁对于发生之前的保证无关紧要.

Yes, the lock used doesn't matter for the happen-before guarantees.

易失性读写是否将所有内存刷新到所有线程,还是仅在两个访问线程之间?无论答案是什么,是所有内存都被刷新还是只有 volatile 变量?

Do volatile read-writes flush all memory to all threads, or only between the two accessing threads? Whichever the answer, is all memory flushed or only the volatile variables?

所有脏内存在写屏障上刷新,所有读取在读屏障上的顺序一致.volatile 在写入时执行写入屏障,在读取时执行读取屏障.

All dirty memory is flushed on a write barrier and all reads will be order consistent on a read barrier. volatile performs both a write barrier on a write and a read barrier on a read.

退出同步块时是否刷新所有更改的内存,还是仅刷新块内更改的内存?

Is all changed memory flushed when exiting a synchronized block, or just the memory that was changed within the block?

该线程更改的所有内存.

All of the memory changed by that thread.

两个线程访问的所有静态变量都必须同步吗?

Do all static variables accessed by two threads have to be synchronized?

仅当一个线程修改变量时.任意数量的线程都可以在不同步的情况下读取静态值.

Only if one thread modifies the variable. Any number of threads can read static values without synchronization.

这篇关于使用 volatile 和 synchronized 时,内存刷新或发布到各个线程的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆