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