【翻译八】java-内存一致性错误
Memory Consistency Errors
Memory consistency errors occur when different threads have inconsistent views of what should be the same data. The causes of memory consistency errors are complex and beyond the scope of this tutorial. Fortunately, the programmer does not need a detailed understanding of these causes. All that is needed is a strategy for avoiding them.
The key to avoiding memory consistency errors is understanding the happens-before relationship. This relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement. To see this, consider the following example. Suppose a simple int field is defined and initialized:
int counter = 0;
The counter field is shared between two threads, A and B. Suppose thread A increments counter:
counter++;
Then, shortly afterwards, thread B prints out counter:
System.out.println(counter);
If the two statements had been executed in the same thread, it would be safe to assume that the value printed out would be "1". But if the two statements are executed in separate threads, the value printed out might well be "0", because there's no guarantee that thread A's change to counter will be visible to thread B — unless the programmer has established a happens-before relationship between these two statements.
There are several actions that create happens-before relationships. One of them is synchronization, as we will see in the following sections.
We've already seen two actions that create happens-before relationships.
When a statement invokes Thread.start, every statement that has a happens-before relationship with that statement also has a happens-before relationship with every statement executed by the new thread. The effects of the code that led up to the creation of the new thread are visible to the new thread.
When a thread terminates and causes a Thread.join in another thread to return, then all the statements executed by the terminated thread have a happens-before relationship with all the statements following the successful join. The effects of the code in the thread are now visible to the thread that performed the join.
For a list of actions that create happens-before relationships, refer to the Summary page of the java.util.concurrent package..
译文:
内存一致性错误
当不同的线程有不一致的访问相同的数据的时候可能会发生内存一致性错误。内存一致性错误的原因是复杂的并且超过了本课程。幸运的是,程序员并不需要知道这个原因的细节。所有需要做的是避免这种问题发生。
避免内存一致性问题发生的关键是找到在它发生之前的关系。这种关系是内存通过指定的语句写入特定的语句的一种根本的保证。为了明白这件事情,考虑一个实例。假如一个简单int域被定义和初始化成如下:
int counter = 0;
conter域被两个线程所共享,A和B.假如A线程增加counter.
counter++;
那么,不久以后,线程B打印出counter.
System.out.println(counter);
如果这两个语句已经在相同的线程执行,那么假设它的执行结果为输出“1”是安全的。但是,如果两个语句被分开了执行。这个执行的值可能为“0”,因为不保证线程A对counter的改变对线程B是可见的,除非程序员已经在这两个语句发生之前就确定了它们之间的关系。
这有很多种方法创建事先的关系。其中一种就是同步方法(synchronization),我们在接下来的章节中会看到。
我们已经看到了两个创建事先的关系的方法。
当一个语句执行thread.start方法的时候。每一个在这个语句发生之前的关系也对应的有一个语句在执行新的线程的时候有这种事先的关系。这种代码的影响导致一个新线程对另外一个新线程是可见的。
当一个线程终止或者引起另外一个线程。连接另外一个线程返回,那么所有的终止执行语句会有一个事先的关系在所有语句在join执行成功之后。现在这种代码的影响导致一个新线程对另外允许连接这个新线程是可见的。
看创建事先的关系的方法,可以参考java.util.concurrent包的总结。
养眼是必须滴^^

【翻译八】java-内存一致性错误的更多相关文章
- Java 多线程之内存一致性错误
当不同的线程针对相同的数据却读到了不同的值时就发生了内存一致性错误.内存一致性错误的原因是非常复杂的.幸运的是我们程序员不需要详细的理解这些原因,我们需要做的事情就是使用策略来规避这些. 避免内存一致 ...
- Java内存一致性
问题 前段时间在做服务注册发现的时候,有一处这样的逻辑,先发现下游服务,然后再是服务启动,服务启动完毕才能注册服务,因为注册一个在启动过程中的服务显然是不正确的设计. 然而很不巧,我们目前使用的TTh ...
- Java内存模型(三)原子性、内存可见性、重排序、顺序一致性、volatile、锁、final
一.原子性 原子性操作指相应的操作是单一不可分割的操作.例如,对int变量count执行count++d操作就不是原子性操作.因为count++实际上可以分解为3个操作:(1)读取变量co ...
- Java内存模型深度解析:顺序一致性--转
原文地址:http://www.codeceo.com/article/java-memory-3.html 数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据 ...
- java内存模型-顺序一致性
数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java 内存模型规范对数据竞争的定义如下: 在一个线程中写一个变量, 在另一个线程读同一个变量, 而且写和读没有通过同步来排序. 当代 ...
- 深入理解Java内存模型(三)——顺序一致性
数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据竞争的定义如下: 在一个线程中写一个变量, 在另一个线程读同一个变量, 而且写和读没有通过同步来排序. 当代码 ...
- 【转】深入理解Java内存模型(三)——顺序一致性
数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据竞争的定义如下: 在一个线程中写一个变量, 在另一个线程读同一个变量, 而且写和读没有通过同步来排序. 当代码 ...
- java中三种常见内存溢出错误的处理方法
更多 10 相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的 ...
- java中三种常见内存溢出错误的处理方法(good)
相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的认识. 在解决j ...
随机推荐
- 如何用Wireshark捕获USB数据?
现在越来越多的电子设备采用USB接口进行通讯,通讯标准也在逐步提高.那么,我们就会好奇这些设备是如何工作的?而无论你是一个硬件黑客,业余爱好者或者只是对它有一点兴趣的,USB对我们都是具有挑战性的. ...
- nginx配置多域名映射方法(本地hosts)
本地测试网站的时候如果不想用localhost/xxxx的形式访问,可能就需要修改hosts文件来映射了,但是一个网站还好,假如有多个网站的话就不行了. 这时就需要配置多域名映射 比如hosts中配置 ...
- C#系统委托之Action And Func
Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...
- 3.nodejs权威指南--文件
1. 文件 1.1 读写整个文件 1.1.1 读 var fs = require('fs'); fs.readFile('./test.txt',function(err,data){ if(err ...
- 2.nodejs权威指南--Buffer
1. Buffer 1.1 创建 var buf1 = new Buffer(100); var buf2 = new Buffer([0,1,2]); var buf3 = new Buffer(' ...
- webpack学习笔记一
主要参考: https://blog.madewithlove.be/post/webpack-your-bags/ 起因: 作为运维狗, 对前端一窍不通但心向往之, 最近做一个Dashboard, ...
- Ubuntu 14.04 下搭建SVN服务器 svn://
Ubuntu 14.04 下搭建SVN服务器 svn:// 安装软件包: sudo apt-get install subversion 之后选择SVN服务文件及配置文件的放置位置.我放在了/srv下 ...
- .NET中的CTS、CLS和CLR
在学习.NET的过程中,都会不可避免地接触到这三个概念,那么这三个东西是什么以及它们之间的关系是怎样的呢?我们在学习的过程中可能比较过多的会去关注CLR,因为CLR是.NET Framework的核心 ...
- hiho一下第二周 Trie树
题目链接:http://hihocoder.com/problemset/problem/1014 #include <iostream> #include <cstdio> ...
- codeforces 492C. Vanya and Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/492/C 题目意思:给出 3 个整数:n, r, avg.然后有 n 行,每行有两个数:第 i 行有 ...