java 线程同步(官方文档翻译)

说明:本人英语水平很差全靠词典,翻译会有很多不准,希望大家指正。

The  Java  programming  language  provides  multiple  mechanisms  for communicating between threads.

Java编程语言提供了多种线程间通信机制。

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. 

Java中的每个对象都是与一个监视器关联,线程可以锁定或解锁该监视器。

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).

如果这个方法是实例方法,同步方法锁定这个被调用方法的实例关联的监视器。(也就是说,这个对象将被称为this当这个方法的方法体在执行过程中)

If the method is  static , it locks the monitor associated with the  Class object that represents the class in which the method is defined.

如果这个方法是静态方法,同步方法锁定Class对象关联的监视器,这个Class对象表示定义这个方法的类。有点绕,Class类是一个泛型类。

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 Java programming language neither prevents nor requires detection of deadlock conditions.

java语言既不预防也不要求检测死锁条件。

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.

在线程持有(直接或间接)的锁在多个对象上的程序中,应该使用常规技术来避免死锁,如有必要,创建不会死锁的高级锁原语。

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的关键字,用于变量定义作用和synchronization类似)变量的读写和java.util.concurrent包中类的使用,提供了可选的同步方式。

---------------------------------------------------------------------------------------------------------------------------------

Every object, in addition to having an associated monitor, has an associated wait set. 

每个对象,创建时有一个关联的监视器和一个关联的等待集合。

A wait set is a set of threads.

一个等待集合是一个线程的集合。
When an object is first created, its wait set is empty. 

当一个一对象首次被创建时,它的等待集合是空的。

Elementary actions that add threads to and remove threads from wait sets are atomic. 

从等待集合中添加和移除线程的基础操作是原子性的。

Wait sets are manipulated solely through the methods  Object.wait ,  Object.notify , and Object.notifyAll .

等待集合只能通过Object.wait 、Object.notify 和Object.notifyAll方法操作。
Wait set manipulations can also be affected by the interruption status of a thread,and by the  Thread class's methods dealing with interruption. 

等待集合操作也可以通过影响线程中断状态,通过Thread类法处理中断的方法。

Additionally, the Thread class's methods for sleeping and joining other threads have properties derived from those of wait and notification actions.

此外,线程类的睡眠和joining其他线程的方法通过属性派生自wait和notification操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值