Java 线程 - 内存一致性错误 [英] Java Thread - Memory consistency errors

查看:58
本文介绍了Java 线程 - 内存一致性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Sun 的并发教程.

但我不明白什么是内存一致性错误?我在谷歌上搜索过,但没有找到任何有用的教程或文章.

But I couldn't understand exactly what memory consistency errors are? I googled about that but didn't find any helpful tutorial or article about that.

我知道这个问题是一个主观问题,因此您可以向我提供有关上述主题的文章的链接.

I know that this question is a subjective one, so you can provide me links to articles on the above topic.

如果能用简单的例子来解释就好了.

It would be great if you explain it with a simple example.

推荐答案

您可以阅读有关 写后读 (RAW)、写后写 (WAW) 和读后写 (WAR) 危害 以了解有关此主题的更多信息.这些危害指的是流水线进程,但它确实与多线程发生的问题相同.这基本上意味着两个不同的线程正在更新内存中的同一位置,如果您以特定顺序依赖这些更新,那么您可能会惊讶地发现您无法保证更新发生的顺序.

You can read up about Read After Write (RAW), Write after Write(WAW) and Write After Read (WAR) hazards to learn more about this topic. These hazards refer to pipelined processses but it really is the same problem that occurs with multi threading. It basically means that two different threads are updating the same location in memory and if you depend on these updates in a certain order then you may be surprised to see that you cannot guarantee the order in which the updates occur.

例如,如果您有两个语句:

For example, if you have two statements:

  x = y + z;
  r = x + z;

在单个线程中,您没有问题,因为 r 的值将始终保持一致.然而,在多线程中,有可能 或者 任何一个语句首先出现并且 r 的值更难预测.

in a single thread then you have no problem because the value of r will always be consistent. In multiple threads however, it is possible or either statement to occur first and the value of r is harder to predict.

这篇关于Java 线程 - 内存一致性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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