当前位置: 首页 > news >正文

同步和Java内存模型(四)有序性

原文链接: http://gee.cs.oswego.edu/dl/cpj/jmm.html

作者:Doug lea 译者:杜建雄  校对者:欧振聪,方腾飞

有序性 

有序性规则表现在以下两种场景: 线程内和线程间

  •  从某个线程的角度看方法的执行,指令会按照一种叫“串行”(as-if-serial)的方式执行,此种方式已经应用于顺序编程语言。
  •  这个线程“观察”到其他线程并发地执行非同步的代码时,任何代码都有可能交叉执行。唯一起作用的约束是:对于同步方法,同步块以及volatile字段的操作仍维持相对有序。

 

再次提醒,这些仅是最小特性的规则。具体到任何一个程序或平台上,可能存在更严格的有序性规则。所以你不能依赖它们,因为即使你的代码遵循了这些更严格的规则,仍可能在不同特性的JVM上运行失败,而且测试非常困难。

需要注意的是,线程内部的观察视角被JLS [1] 中其他的语义的讨论所采用。例如,算术表达式的计算在线程内看来是从左到右地执行操作(JLS 15.6章节),而这种执行效果是没有必要被其他线程观察到的。

仅当某一时刻只有一个线程操作变量时,线程内的执行表现为串行。出现上述情景,可能是因为使用了同步,互斥体[2] 或者纯属巧合。当多线程同时运行在非同步的代码里进行公用字段的读写时,会形成一种执行模式。在这种模式下,代码会任意交叉执行,原子性和可见性会失效,以及产生竞态条件。这时线程执行不再表现为串行。

尽管JLS列出了一些特定的合法和非法的重排序,如果碰到所列范围之外的问题,会降低以下这条实践保证 :运行结果反映了几乎所有的重排序产生的代码交叉执行的情况。所以,没必要去探究这些代码的有序性。

译注:

【1】JLS:Java Language Specification ,Java语言规范

【2】互斥体:原文为structural exclusion,译者认为意同 mutual exclusion ,详见 互斥体。

原文

Synchronization and the Java Memory Model

Ordering

Ordering rules fall under two cases, within-thread and between-thread:

  • From the point of view of the thread performing the actions in a method, instructions proceed in the normal as-if-serial manner that applies in sequential programming languages.
  • From the point of view of other threads that might be “spying” on this thread by concurrently running unsynchronized methods, almost anything can happen. The only useful constraint is that the relative orderings of synchronized methods and blocks, as well as operations on volatile fields, are always preserved.

Again, these are only the minimal guaranteed properties. In any given program or platform, you may find stricter orderings. But you cannot rely on them, and you may find it difficult to test for code that would fail on JVM implementations that have different properties but still conform to the rules.

Note that the within-thread point of view is implicitly adopted in all other discussions of semantics in JLS. For example, arithmetic expression evaluation is performed in left-to-right order (JLS section 15.6) as viewed by the thread performing the operations, but not necessarily as viewed by other threads.

The within-thread as-if-serial property is helpful only when only one thread at a time is manipulating variables, due to synchronization, structural exclusion, or pure chance. When multiple threads are all running unsynchronized code that reads and writes common fields, then arbitrary interleavings, atomicity failures, race conditions, and visibility failures may result in execution patterns that make the notion of as-if-serial just about meaningless with respect to any given thread.

Even though JLS addresses some particular legal and illegal reorderings that can occur, interactions with these other issues reduce practical guarantees to saying that the results may reflect just about any possible interleaving of just about any possible reordering. So there is no point in trying to reason about the ordering properties of such code. 

相关文章:

  • git命令的简单使用
  • lintcode:买卖股票的最佳时机 I
  • PHP处理一个5G文件,使用内存512M的,数据为整形,从大到小排序,优化排序算法...
  • c++的this指针
  • CM android的CMUpdater分析(二)
  • Saving HDU hdu
  • activiti 动态配置 activiti 监听引擎启动和初始化(高级源码篇)
  • Webview组件和HTML的介绍
  • [一句秒懂]高仿QQ底部小红点弹簧效果
  • 安全关闭多Activity的Application
  • Android自定义控件之日历控件
  • path去除多余“/”和添加“/”正则
  • JS魔法堂:doctype我们应该了解的基础知识
  • CSS3魔法堂:禁止用户改变textarea大小
  • 音效
  • [数据结构]链表的实现在PHP中
  • js
  • js 实现textarea输入字数提示
  • JS字符串转数字方法总结
  • mac修复ab及siege安装
  • miaov-React 最佳入门
  • PhantomJS 安装
  • python学习笔记 - ThreadLocal
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • springMvc学习笔记(2)
  • SwizzleMethod 黑魔法
  • vue-router的history模式发布配置
  • 对JS继承的一点思考
  • 经典排序算法及其 Java 实现
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 思维导图—你不知道的JavaScript中卷
  • 为视图添加丝滑的水波纹
  • 我建了一个叫Hello World的项目
  • 携程小程序初体验
  • 一道闭包题引发的思考
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • ​secrets --- 生成管理密码的安全随机数​
  • #FPGA(基础知识)
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (13):Silverlight 2 数据与通信之WebRequest
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (强烈推荐)移动端音视频从零到上手(下)
  • (一) springboot详细介绍
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • .NET 常见的偏门问题
  • .net 无限分类
  • @Async注解的坑,小心
  • [AR]Vumark(下一代条形码)
  • [C# WPF] DataGrid选中行或选中单元格的背景和字体颜色修改
  • [C#]使用DlibDotNet人脸检测人脸68特征点识别人脸5特征点识别人脸对齐人脸比对FaceMesh
  • [EFI]Lenovo ThinkPad X280电脑 Hackintosh 黑苹果引导文件
  • [Head First设计模式]策略模式
  • [iOS开发]事件处理与响应者链