While most of the discussion in the preceding chapters is concerned only with the behavior of code as executed a single statement or expression at a time, that is, by a single thread, the Java Virtual Machine can support many threads of execution at once. These threads independently execute code that operates on values and objects residing in a shared main memory. Threads may be supported by having many hardware processors, by time
-
slicing a single hardware processor, or by time
-
slicing many hardware processors.
虽然前面章节大多数的讨论仅涉及到同一时间只有单个语句或表达式执行的行为,也就是说单线程(程序);但是,JVM(Java虚拟机)支持同一时间执行多个线程。这些线程单独地执行各自的代码、操作分配在
共享主内存中的值(values)和对象(objects)。多线程可以被多个硬件处理器,或者单硬件处理器时间碎片,或者多硬件处理器时间碎片支持。
Threads are represented by the Thread class. 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.
线程是通过Thread类来表示。对用户来说,创建一个线程的唯一方式就是创建一个Thread的对象;每一个线程与一个Thread对象关联。当调用相应线程对象的start() 方法时,该线程将被启动。
The behavior of threads, particularly when not correctly synchronized, can be confusing and counterintuitive. This chapter describes the semantics of multithreaded programs; 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 known as the Java programming language memory model. When no confusion can arise, we will simply refer to these rules as "the memory model".
多线程的行为,尤其是当没有正确同步,可能会造成混淆和有悖常理。本章介绍了多线程程序的语义;它包含的规则,保证对被多个线程更新的共享内存中的值的读操作是可见的。如同该规范与不同硬件架构体系的内存模型类似一样,这些语义以Java语言内存模型的概念而被熟知。在不引起混淆的情况下,我们简单的将这些规则称作“
内存模型”。
These semantics do not prescribe 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.
这些语义没有规定多线程程序应该如何被执行。相反,它们描述了多线程程序被允许表现的行为。不管什么执行策略,只有生成被允许行为的策略才是一个可接受的执行策略。
17.1. Synchronization 同步
The Java programming language provides multiple mechanisms for communicating between threads. The most basic of these methods is synchronization, which is implemented using monitors. Each object in Java 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.
Java 编程语言为线程间通信提供了多种机制。这些方法最基本的同步,它是通过
监视器来实现的。Java 中的任何一个对象与一个监视器关联,线程可以对该监视器进行加锁和解锁操作。同一时间在同一个监视器上只有一个线程可以持有一个锁。任何其他想获得该监视器锁的线程将一直被阻塞,直到它们获得了该监视器的一个锁。一个线程t可能锁定一个特殊的监视器多次,每一次解锁反转一个锁定操作的效果。
The synchronized statement (§14.19) 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.
该 synchronized语句在一个对象上计算一次引用;然后在该对象的监视器上尝试执行一个加锁操作,这时线程不会继续执行,直到锁操作成功完成。锁操作执行后,同步语句块将被执行。如果执行块完成,不管正常地或是意外地,在该监视器上一个解锁操作都会自动执行。
A synchronized method (§8.4.3.6) 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.
一个同步方法自动执行一个锁操作当它被调用时;方法体不会被执行,直到锁操作已经成功完成。如果是实例同步方法,那么它将锁定与监视器关联的实例,该实例就是拥有该被调用的同步方法的实例。(也就是说,在方法体执行期间,该对象是以this表示而知道的)。如果该方法是一个静态实例方法,它锁定的值该监视器关联的Class对象,该Class对象表示该静态同步方法在其定义的class。如果方法体的执行完成,不管是正常地或异常地,一个解锁操作都将自动在该监视器上执行。
The Java programming language 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 do not deadlock, if necessary.
Java 编程语言既不防止也不要求对死锁条件进行检测。有多个线程在多个对象上持有(直接或间接)锁的程序应该使用惯用的技术避免死锁,如果有必要,创建高级的不会死锁的锁原语。
Other mechanisms, such as reads and writes of volatile variables and the use of classes in the java.util.concurrent package, provide alternative ways of synchronization.
其他机制
提供了可选的同步方法
,比如volatile变量的读写和java.util.concurrent包中类的使用。