日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java重排序_Java内存模型FAQ(四)重排序意味着什么?

發(fā)布時(shí)間:2023/12/2 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java重排序_Java内存模型FAQ(四)重排序意味着什么? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

譯者:Alex

在很多情況下,訪問(wèn)一個(gè)程序變量(對(duì)象實(shí)例字段,類靜態(tài)字段和數(shù)組元素)可能會(huì)使用不同的順序執(zhí)行,而不是程序語(yǔ)義所指定的順序執(zhí)行。編譯器能夠自由的以優(yōu)化的名義去改變指令順序。在特定的環(huán)境下,處理器可能會(huì)次序顛倒的執(zhí)行指令。數(shù)據(jù)可能在寄存器,處理器緩沖區(qū)和主內(nèi)存中以不同的次序移動(dòng),而不是按照程序指定的順序。

例如,如果一個(gè)線程寫入值到字段a,然后寫入值到字段b,而且b的值不依賴于a的值,那么,處理器就能夠自由的調(diào)整它們的執(zhí)行順序,而且緩沖區(qū)能夠在a之前刷新b的值到主內(nèi)存。有許多潛在的重排序的來(lái)源,例如編譯器,JIT以及緩沖區(qū)。

編譯器,運(yùn)行時(shí)和硬件被期望一起協(xié)力創(chuàng)建好像是順序執(zhí)行的語(yǔ)義的假象,這意味著在單線程的程序中,程序應(yīng)該是不能夠觀察到重排序的影響的。但是,重排序在沒(méi)有正確同步了的多線程程序中開(kāi)始起作用,在這些多線程程序中,一個(gè)線程能夠觀察到其他線程的影響,也可能檢測(cè)到其他線程將會(huì)以一種不同于程序語(yǔ)義所規(guī)定的執(zhí)行順序來(lái)訪問(wèn)變量。

大部分情況下,一個(gè)線程不會(huì)關(guān)注其他線程正在做什么,但是當(dāng)它需要關(guān)注的時(shí)候,這時(shí)候就需要同步了。

原文

What is meant by reordering?

There are a number of cases in which accesses to program variables (object instance fields, class static fields, and array elements) may appear to execute in a different order than was specified by the program. The compiler is free to take liberties with the ordering of instructions in the name of optimization. Processors may execute instructions out of order under certain circumstances. Data may be moved between registers, processor caches, and main memory in different order than specified by the program.

For example, if a thread writes to field?a?and then to field?b, and the value of?b?does not depend on the value of?a, then the compiler is free to reorder these operations, and the cache is free to flush?b?to main memory before?a. There are a number of potential sources of reordering, such as the compiler, the JIT, and the cache.

The compiler, runtime, and hardware are supposed to conspire to create the illusion of as-if-serial semantics, which means that in a single-threaded program, the program should not be able to observe the effects of reorderings. However, reorderings can come into play in incorrectly synchronized multithreaded programs, where one thread is able to observe the effects of other threads, and may be able to detect that variable accesses become visible to other threads in a different order than executed or specified in the program.

Most of the time, one thread doesn’t care what the other is doing. But when it does, that’s what synchronization is for.

總結(jié)

以上是生活随笔為你收集整理的java重排序_Java内存模型FAQ(四)重排序意味着什么?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。