Java記憶體模型FAQ(三)JSR133是什麼?

喝水會長肉發表於2021-12-02

從1997年以來,人們不斷發現Java語言規範的17章定義的Java記憶體模型中的一些嚴重的缺陷。這些缺陷會導致一些使人迷惑的行為(例如final欄位會被觀察到值的改變)和破壞編譯器常見的優化能力。

Java記憶體模型是一個雄心勃勃的計劃,它是程式語言規範第一次嘗試合併一個能夠在各種處理器架構中為併發提供一致語義的記憶體模型。不過,定義一個既一致又直觀的記憶體模型遠比想象要更難。JSR133為Java語言定義了一個新的記憶體模型,它修復了早期記憶體模型中的缺陷。為了實現JSR133,final和volatile的語義需要重新定義。

完整的語義見:,但是正式的語義不是小心翼翼的,它是令人驚訝和清醒的,目的是讓人意識到一些看似簡單的概念(如同步)其實有多複雜。幸運的是,你不需要懂得這些正式語義的細節——JSR133的目的是建立一組正式語義,這些正式語義提供了volatile、synchronzied和final如何工作的直觀框架。

JSR 133的目標包含了:

  • 保留已經存在的安全保證(像型別安全)以及強化其他的安全保證。例如,變數值不能憑空建立:執行緒觀察到的每個變數的值必須是被其他執行緒合理的設定的。

  • 正確同步的程式的語義應該儘量簡單和直觀。

  • 應該定義未完成或者未正確同步的程式的語義,主要是為了把潛在的安全危害降到最低。

  • 程式設計師應該能夠自信的推斷多執行緒程式如何同記憶體進行互動的。

  • 能夠在現在許多流行的硬體架構中設計正確以及高效能的JVM實現。

  • 應該能提供 安全地初始化的保證。如果一個物件正確的構建了(意思是它的引用沒有在構建的時候逸出,那麼所有能夠看到這個物件的引用的執行緒,在不進行同步的情況下,也將能看到在構造方法中中設定的final欄位的值。

  • 應該儘量不影響現有的程式碼。

  • 《Java記憶體模型FAQ(一) 什麼是記憶體模型》


    原文

    What is JSR 133 about?

    Since 1997, several serious flaws have been discovered in the Java Memory Model as defined in Chapter 17 of the Java Language Specification. These flaws allowed for confusing behaviors (such as final fields being observed to change their value) and undermined the compiler’s ability to perform common optimizations.

    The Java Memory Model was an ambitious undertaking; it was the first time that a programming language specification attempted to incorporate a memory model which could provide consistent semantics for concurrency across a variety of architectures. Unfortunately, defining a memory model which is both consistent and intuitive proved far more difficult than expected. JSR 133 defines a new memory model for the Java language which fixes the flaws of the earlier memory model. In order to do this, the semantics of final and volatile needed to change.

    The full semantics are available at , but the formal semantics are not for the timid. It is surprising, and sobering, to discover how complicated seemingly simple concepts like synchronization really are. Fortunately, you need not understand the details of the formal semantics — the goal of JSR 133 was to create a set of formal semantics that provides an intuitive framework for how volatile, synchronized, and final work.

    The goals of JSR 133 include:

    • Preserving existing safety guarantees, like type-safety, and strengthening others. For example, variable values may not be created “out of thin air”: each value for a variable observed by some thread must be a value that can reasonably be placed there by some thread.

    • The semantics of correctly synchronized programs should be as simple and intuitive as possible.

    • The semantics of incompletely or incorrectly synchronized programs should be defined so that potential security hazards are minimized.

    • Programmers should be able to reason confidently about how multithreaded programs interact with memory.

    • It should be possible to design correct, high performance JVM implementations across a wide range of popular hardware architectures.

    • A new guarantee of initialization safety should be provided. If an object is properly constructed (which means that references to it do not escape during construction), then all threads which see a reference to that object will also see the values for its final fields that were set in the constructor, without the need for synchronization.

    • There should be minimal impact on existing code.




    來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70010294/viewspace-2845375/,如需轉載,請註明出處,否則將追究法律責任。

    相關文章