1 Introduction
Java virtual machines support multiple threads of execution. Threads are represented in
Java by the Thread class. The only way for a user to create a thread is to create an object
of this class; each Java thread is associated with s uch an object. A thread will start when
the start() method is invoked on the corresponding Thread object.
The b ehavior of threads, particularly when not correctly s ynchronized, can be confus-
ing and counterintuitive. This specification describes the semantics of multithreaded Java
programs, including 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 Java memory model.
These semantics do not describe how a multithreaded program should be executed.
Rather, they describe only the behaviors that are allowed by multithreaded programs. Any
execution strategy that generates only allowed behaviors is an acceptable execution strategy.
1.1 Locks
Java 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.
The Java programming language does not provide a way to perform separate lock and
unlock actions; instead, they are implicitly performed by high-level constructs that always
arrange to pair such actions correctly.
Note, however, that the Java virtual machine provides separate monitorenter and moni-
torexit instructions that implement the lock and unlock actions.
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 successf ully completed. If the method is
an instance method, it locks the monitor associated with the instance f or 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.
5
评论1
最新资源