Memory Consistency Erros

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:

避免内存一致性错误的关键是了解happens-before关系。这种关系简单的保证了一个特定语句对内存的写操作对另外一个特定语句是可见的。看看这个,考虑下面的例子。假设定义和初始化了整型域

int counter = 0;

The counter field is shared between two threads, A and B. Suppose thread A increments counter:

counter域在A和B,2个线程间共享。假设线程A做了一个自加操作

counter++;

Then, shortly afterwards, thread B prints out counter:

然后,很快,线程B打印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.

如果这2个语句在同一线程里执行,可以确定会打印出“1”。但是如果这2个语句是发生在单独的线程里,打印出的值可能为“0”,因为不能保证线程A对counter的改变对线程B是可见的-除非程序员在这2个语句间建立一种happens-before关系。

There are several actions that create happens-before relationships. One of them is synchronization, as we will see in the following sections.

有几种方式可以建立happens-before关系。同步(synchronization)是其中的一种,我们将在下面的章节学习到。

We've already seen two actions that create happens-before relationships.

我们已经学习了2种建立happens-before关系的方法

  • 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.
  • 当一个语句调用Thread.start,每个与这个语句(Thread.start)有happens-before关系的语句也与新线程中执行的语句有happens-before关系。这个代码(指Thread.start)的效果就是新线程的创建对新线程是可见的(我的理解是:A语句happens-before于Thread.start,Thread.start语句happens-before于新线程中的语句,so,A语句happens-before于新线程中的语句)
  • 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.
  • 当一个线程终止并且导致Thread.join在另外一个线程中返回(return),在终止线程中执行的所有语句都happens-before于紧接在join(成功执行)方法后面的语句。这个代码(join)在线程中的效果对于执行join方法的线程是可见的。(线程调用join方法后,只有在线程终止后才会机会往下执行)

For a list of actions that create happens-before relationships, refer to the Summary page of the java.util.concurrent package..

posted @ 2012-05-23 22:21  yuwenxing  阅读(241)  评论(0编辑  收藏  举报