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

JSR-133: JavaTM Memory Model and Thread Specification原文解析

http://www.cs.umd.edu/~pugh/java/memoryModel/jsr133.pdf

1 Introduction

在这里插入图片描述

JavaTM virtual machines support multiple threads of execution.
java虚拟机支持多线程执行。

Threads are represented by the Thread class.
线程由Thread类表示。

The only way for a user to create a thread is to create an object of this class;
用户创建线程的唯一方法是创建此类的对象;

each thread is associated with such an object.
每个线程都与这样的对象关联。

A thread will start when the start() method is invoked on the corresponding Thread object.
当对相应的线程对象调用start()方法时,线程将启动。

The behavior of threads, particularly when not correctly synchronized, can be confusing and
counterintuitive.
线程的行为,特别是在未正确同步时,可能会令人困惑这是违反直觉的。
counterintuitive 违反直觉
confusing感到困惑

This specification describes the semantics of multithreaded programs written in the JavaTM programming language;
本规范描述了用JavaTM编程语言编写的多线程程序的语义;

it includes rules for which values may be seen by a read of shared memory that is updated by multiple threads.
它包括可通过读取共享内存(由多个线程更新)来查看其值的规则。

As the specification is similar to the memory models for different hardware architectures, these semantics are referred to as the JavaTM memory model.
由于规范类似于不同硬件架构的内存模型,这些语义被称为JavaTM内存模型。

These semantics do not describe how a multithreaded program should be executed.
这些语义并不描述多线程程序应该如何执行。

Rather, they describe the behaviors that multithreaded programs are allowed to exhibit.
相反,它们描述了允许多线程程序显示的行为。

Any execution strategy that generates only allowed behaviors is an acceptable execution strategy.
任何只生成允许的行为的执行策略都是可接受的执行策略。

1.1 Locks

在这里插入图片描述
There are multiple mechanisms for communicating between threads.
线程之间的通信有多种机制。

The most basic of these methods is synchronization, which is implemented using monitors.
这些方法中最基本的是同步,它是使用监视器实现的.

Each object is associated with a monitor, which a thread can lock or unlock.
每个对象都与监视器关联,线程可以锁定或解锁监视器。

Only one thread at a time may hold a lock on a monitor.
一次只有一个线程可以在监视器上保持锁。

Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor.
试图锁定该监视器的任何其他线程都将被阻止,直到它们可以在该监视器上获得锁定为止。

A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation.
线程t可以多次锁定特定监视器;每次解锁都会反转一次锁定操作的效果。

The synchronized statement computes a reference to an object;
同步语句计算对对象的引用;

it then attempts to perform a lock action on that object’s monitor and does not proceed further until the lock action has
successfully completed.
然后,它尝试在该对象的监视器上执行锁定操作,并且在锁定操作完成之前不会继续已成功完成。

After the lock action has been performed, the body of the synchronized statement is executed.
执行锁定操作后,执行同步语句的主体。

If execution of the body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.
如果正常或突然完成主体的执行,则在同一监视器上自动执行解锁操作。

A synchronized method automatically performs a lock action when it is invoked;
同步方法在被调用时自动执行锁定动作;

its body is not executed until the lock action has successfully completed.
在锁定操作成功完成之前,不会执行其主体。

If the method is an instance method, it locks the monitor associated with the instance for which it was invoked (that is, the object that
will be known as this during execution of the body of the method).
如果该方法是实例方法,它将锁定与调用该方法的实例(即在方法主体的执行期间将被称为这一点)。

If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined.
如果该方法是静态的,它将锁定与表示定义该方法的类的类对象关联的监视器。

If execution of the method’s body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.
如果方法体的执行完成了,不管是正常地还是突然地,解锁动作会在同一个监视器上自动执行。

The semantics neither prevents nor requires detection of deadlock conditions.
该语义既不防止也不要求检测死锁条件。

Programs where threads hold (directly or indirectly) locks on multiple objects should use conventional techniques for deadlock avoidance, creating higher-level locking primitives that don’t deadlock, if necessary.
线程在多个对象上持有(直接或间接)锁的程序应该使用传统的技术来避免死锁,必要时创建不会死锁的高级锁原语。

Other mechanisms, such as reads and writes of volatile variables and classes provided in the java.util.concurrent package, provide alternative mechanisms for synchronization.
其他机制,如java.util.concurrent包中提供的可变变量和类的读写,为同步提供了替代机制。

1.2 Notation in Examples

在这里插入图片描述
在这里插入图片描述图1:语句重新排序导致的令人惊讶的结果

The Java memory model is not fundamentally based in the object oriented nature of the Java programming language.
Java内存模型并不完全基于Java的面向对象特性 编程语言。

For conciseness and simplicity in our examples, we often exhibit code fragments without class or method definitions, or explicit dereferencing.
在我们的例子中,为了简洁和简单,我们经常展示没有类或方法定义或者显式解引用的代码片段。

Most examples consist of two or more threads containing statements with access to local variables, shared global variables or
instance fields of an object.
大多数例子由两个或多个线程组成,这些线程包含可以访问局部变量、共享全局变量或 对象的实例字段。

We typically use variables names such as r1 or r2 to indicate variables local to a method or thread. Such variables are not accessible by other threads.
我们通常使用变量名称,如r1或r2,来表示方法或线程的局部变量。其他线程无法访问这些变量。

2 Incorrectly Synchronized Programs Exhibit Surprising Behaviors

不正确同步的程序表现出令人惊讶的行为

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.
Java编程语言的语义允许编译器和微处理器执行优化,这些优化会与不正确同步的代码交互,从而产生看似矛盾的行为。

相关文章:

  • html网页如何获取后台数据库的数据(html + ajax + php + mysql)
  • Spring之事务实现原理及其注解@Transactional底层和传播机制原理
  • 第14章: 集合
  • Java后端开发工程师学习笔记【狂神说Java笔记】
  • Linux上的中文输入法安装(Ubuntu + Kali五笔拼音)
  • java+php+python的公文审批系统-办公系统
  • 使用C语言+USRP B210从零开始实现无线通信(4) 接收检测与解调
  • Spark系列从入门到精通(二)
  • hive之full outer join(全连接)使用方法
  • Hamiton图系列文章 (1) Hamilton图证明算法的道路矩阵基础数据结构与类的设计
  • 计算机毕业设计ssm+vue基于微信的产品订单管理小程序
  • HyperLynx(十七)SATA的设计与仿真
  • JavaWeb在线商城系统(java+jsp+servlet+MySQL+jdbc+css+js+jQuery)
  • [Java]快速入门优先队列(堆)手撕相关面试题
  • Ubuntu22.04安装CUDA深度学习环境cuda principle
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • android图片蒙层
  • C# 免费离线人脸识别 2.0 Demo
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • JavaScript HTML DOM
  • linux安装openssl、swoole等扩展的具体步骤
  • MySQL数据库运维之数据恢复
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • python学习笔记 - ThreadLocal
  • sublime配置文件
  • vue数据传递--我有特殊的实现技巧
  • Xmanager 远程桌面 CentOS 7
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 翻译--Thinking in React
  • 后端_ThinkPHP5
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 协程
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • 06-01 点餐小程序前台界面搭建
  • 【干货分享】dos命令大全
  • Java性能优化之JVM GC(垃圾回收机制)
  • 从如何停掉 Promise 链说起
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (C语言)fread与fwrite详解
  • (ibm)Java 语言的 XPath API
  • (MATLAB)第五章-矩阵运算
  • (八十八)VFL语言初步 - 实现布局
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (一)SpringBoot3---尚硅谷总结
  • (转)jdk与jre的区别
  • *上位机的定义
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .Net MVC4 上传大文件,并保存表单