Synchronized Methods

Synchronized Methods
同步方法
The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements. The more complex of the two, synchronized statements, are described in the next section. This section is about synchronized methods.
java编程语句提供了2种基本的同步习语(idioms)。同步方法(synchronized methods)和同步语句(synchronized statements)。第二种更复杂,同步语句(synchronized statements)在下一章介绍。这一章是关于同步方法(synchronized methods)


To make a method synchronized, simply add the synchronized keyword to its declaration:
简单的在方法的声明上加上synchronized关键字就可以使方法同步
public class SynchronizedCounter {
private int c = 0;

public synchronized void increment() {
c++;
}

public synchronized void decrement() {
c--;
}

public synchronized int value() {
return c;
}
}
If count is an instance of SynchronizedCounter, then making these methods synchronized has two effects:
如果count是SynchronizedCounter类的一个实例,使这三个方法同步有2个效果
First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
第一,同一个对象上的同步方法不可能交错执行。当一个线程(这里我们叫线程A)正在执行一个对象(假设叫对象B)的同步方法时,所有其他的线程直到第一个线程(线程A)中的(对象B的)同步方法执行完毕才回调用(B对象中的)同步方法块
Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
Note that constructors cannot be synchronized — using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.
第二,当一个同步方法退出,它会自动与随后调用的相同对象中的同步方法建立一种happens-before关系。这保证了对象状态的改变对于其他线程是可见的。
注意构造方法(constructs)不能够被同步-在构造器(construct)上使用synchonized关键字是一种语法错误。同步构造器也没有任何意义,因为只有线程在创建对象时才会使用它


--------------------------------------------------------------------------------
Warning: When constructing an object that will be shared between threads, be very careful that a reference to the object does not "leak" prematurely. For example, suppose you want to maintain a List called instances containing every instance of class. You might be tempted to add the following line to your constructor:
instances.add(this);
But then other threads can use instances to access the object before construction of the object is complete.
警告:一个正在构造的对象将在线程间共享,必须非常小心不要过早(prematurely)的泄露对象的引用。比如,假设你想维护一个叫instances包含每个类实例的List。你可能想在你的代码中增加下面的代码
instances.add(this);
但是其他的对象会在你对象构造完成前使用instances访问你正在构造的对象。
--------------------------------------------------------------------------------

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. (An important exception: final fields, which cannot be modified after the object is constructed, can be safely read through non-synchronized methods, once the object is constructed) This strategy is effective, but can present problems with liveness, as we'll see later in this lesson.
同步方法是一种简单的防止线程冲突(thread interference)和线程一致性错误(memory consistency errors)的策略:如果一个对象对多个对象可见。所有针对对象变量的读写都会通过同步方法。(一个重要的例外:final fields,在线程构造完毕后不能被更改,一旦对象被构造,能够通过非同步方法访问)。这种策略是有效的,但是会存在活性(liveness)问题,我们将在下章讨论这个

 

posted @ 2012-05-24 20:16  yuwenxing  阅读(306)  评论(0编辑  收藏  举报