Spec_JSR133

image-20190616103858903

Introduction

Memory Model and Thread Specification , 内存模型和线程规范

These semantics do not describe how a multithreaded program should be executed. Rather,they describe the behaviors that multithreaded programs are allowed to exhibit

规范的标准内容将合并到JavaTM语言规范JavaTM虚拟机规范以及java.lang包的类说明中

因为线程的行为,尤其是未正确同步时的行为,可能会让人困惑和违背直觉 ,所以规范规定了

  • 用于屏蔽掉各种硬件和操作系统的内存访问差异,以实现让Java程序在各种平台下都能达到一致的并发效果
  • 规定了一个线程如何和何时可以看到由其他线程修改过后的共享变量的值,以及在必须时如何同步的访问共享变量。

Locks

There are multiple mechanisms for communicating between threads.most basic of these
methods is synchronization, which is implemented using monitors.

Each object is associated witha monitor, which a thread can lock or unlock.

Only one thread at a time may hold a lock on amonitor.

Any other threads attempting to lock that monitor are blocked until they can obtain a
lock on that monitor.

Other mechanisms, such as reads and writes of volatile variables and classes provided in the
java.util.concurrent package, provide alternative mechanisms for synchronization.

同步语义的实现 synchronized 的实现,volatile,juc 包

Incorrectly Synchronized Programs Exhibit Surprising Behav-iors

The semantics of the Java programming language allow compilers and microprocessors to perform optimizations that can interact with incorrectly synchronized code in ways that can produce behaviors that seem paradoxical.

However,it was forbidden by the original Java memory model in the JLS and JVMS: this was one of the first indications that the original JMM needed to be replaced.

Java 的语义允许编译器和微处理器进行优化、重排 会改变语义,所以需要重新定义JMM内存模型

Informal Semantics

There are two key ideas to understanding whether a program is correctly synchronized:

  1. Conflicting Accesses
  2. Happens-Before Relationship

There are a number of ways to induce a happens-before ordering, including:

  • Each action in a thread happens-before every subsequent action in that thread. • An unlock on a monitor happens-before every subsequent lock on that monitor. • A write to a volatile field happens-before every subsequent read of that volatile.

  • A call to start() on a thread happens-before any actions in the started thread.

  • All actions in a thread happen-before any other thread successfully returns from a join() on that thread.

  • If an action a happens-before an action b, and b happens before an action c, then a happens- before c.

When a program contains two conflicting accesses that are not ordered by a happens-before relationship, it is said to contain a data race.

理解一个程序是否被正确的同步了,有两个关键概念: 冲突访问和H-B

Sequential Consistency

As noted before, sequential consistency and/or freedom from data races still allows errors arising from
groups of operations that need to be perceived atomically and are not.

If we were to use sequential consistency as our memory model, many of the compiler and
processor optimizations that we have discussed would be illegal.

Final Fields

顺序一致性模型 也不能解决语义问题,同时还会让优化失效

What is a Memory Model?

A memory model describes, given a program and an execution trace of that program, whether
the execution trace is a legal execution of the program. For the Java programming language, the
memory model works by examining each read in an execution trace and checking that the write
observed by that read is valid according to certain rules.

内存模型 描述程序的执行行为是否合法 对于Java 来说,内存共享型, 需要关注的是当前线程读取到的信息是否是最新的, 因为各个线程都有一个本地缓存(栈、cpu缓存),通过堆来共享

img

Definitions

Shared variables/Heap memory 共享变量/堆内存

Inter-thread Actions 线程间的动作

Program Order 程序顺序

Intra-thread semantics 线程内语义

Synchronization Actions 同步动作

Synchronization Order 同步顺序

Happens-Before and Synchronizes-With Edges Happens-Before 与 Synchronizes-With 边缘

synchronizes-with,同步动作上的偏序关系。

happens-before,动作上的偏序关系。

Approximations to a Java Memory Model

  • Sequential Consistency Memory Model
  • Happens-Before Memory Model
  • Causality
    • Happens-Before is too Weak
    • Causality is Subtle

顺序一致性模型 约束太强了,不利于优化

Happens-Before 太弱了,执行结果不可预测

Happens-before 模型最致命的弱点是其允许值“凭空出现(out of thin air)”

Word Tearing

One implementation consideration for Java virtual machines is that every field and array element
is considered distinct; updates to one field or element must not interact with reads or updates
of any other field or element. In particular, two threads that update adjacent elements of a byte
array separately must not interfere or interact and do not need synchronization to ensure sequential
consistency.

Non-atomic Treatment of double and long

For efficiency’s sake, this
behavior is implementation specific; JavaTM virtual machines are free to perform writes to long
and double values atomically or in two parts.

Final Field Semantics 、Fairness、Wait Sets and Notification、Sleep and Yield、Finalization

source :http://www.cs.umd.edu/~pugh/java/memoryModel/jsr133.pdf?spm=a2c4e.10696291.0.0.3c5419a4WH2pe1&file=jsr133.pdf


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 951488791@qq.com

文章标题:Spec_JSR133

字数:1.1k

本文作者:zhengyumin

发布时间:2019-06-14, 19:26:57

最后更新:2020-01-12, 23:12:06

原始链接:http://zyumin.github.io/2019/06/14/Spec-JSR133/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。