【翻译八】java-内存一致性错误

时间:2022-03-12 19:21:14

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-内存一致性错误