There were several serious problems with the old memory model. It was difficult to understand, and therefore widely violated. For example, the old model did not, in many cases, allow the kinds of reorderings that took place in every JVM. This confusion about the implications of the old model was what compelled the formation of JSR-133.程序員
舊的內存模型有一些嚴重的問題,也很是難理解,所以總被違反。舉個栗子,多數狀況下,舊模型不容許任何JVM中發生重排序。但實際上重排序又是存在的,正是這種混亂,促使了JSR-133的編制。多線程
One widely held belief, for example, was that if final fields were used, then synchronization between threads was unnecessary to guarantee another thread would see the value of the field. While this is a reasonable assumption and a sensible behavior, and indeed how we would want things to work, under the old memory model, it was simply not true. Nothing in the old memory model treated final fields differently from any other field -- meaning synchronization was the only way to ensure that all threads see the value of a final field that was written by the constructor. As a result, it was possible for a thread to see the default value of the field, and then at some later time see its constructed value. This means, for example, that immutable objects like String can appear to change their value -- a disturbing prospect indeed.app
再舉個栗子,在舊的內存模型下的多線程中,若是使用了final字段,就不必使用synchronized來保證該字段的可見性了。但這只是個合理的假想,也是個合理的作法,事實上,這與咱們想要的程序徹底不一樣。舊內存模型下,final字段與其餘字段沒有任何不一樣,這意味着,在構造方法中賦值的final字段,必須用同步來保證其對其餘線程的可見性。所以,在舊的內存模型下,當對象正在構建時,其餘線程會先看到對象中final字段的默認值,而後會看到它在構造方法中被賦予的值,這意味着,本應不可變的對象的值卻會改變,例如String。確實好亂。ide
The old memory model allowed for volatile writes to be reordered with nonvolatile reads and writes, which was not consistent with most developers intuitions about volatile and therefore caused confusion.ui
舊內存模型容許volatile字段的寫入和非volatile字段的讀寫發生重排序,這與大多開發者對volatile的直覺不一致,所以也會產生疑惑。 此處大概是這樣的this
volatile int a = 1; int b = 0; //b多是1,也多是0 b = ++a;
Finally, as we shall see, programmers' intuitions about what can occur when their programs are incorrectly synchronized are often mistaken. One of the goals of JSR-133 is to call attention to this fact.線程
最終,正如咱們看到的,程序員對未正確同步的程序運行的直觀判斷常常是錯的,JSR-133的其中一個目的,就是爲了引發程序員們對這個事實的注意。code