【翻译九】java-同步方法

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.

  To make a method synchronized, simply add the synchronized keyword to its declaration:

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:

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

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

译文:

同步方法
  Java语句提供了两种基本的synchronization方式:synchronized方法和synchronized语句。这两种方法比较复杂的,synchronized语句,将在下一节中进行描述。这一节描述的是synchronized方法。
  为了写一个synchronized方法,只要在申明的时候加synchronized关键字就行了。

 1 public class SynchronizedCounter {

 2 private int c = 0;

 3 

 4 public synchronized void increment() {

 5     c++;

 6     }

 7 

 8 public synchronized void decrement() {

 9     c--;

10     }

11 

12 public synchronized int value() {

13     return c;

14     }

15 }

  如果count是SynchronizedCounter的一个实例,那么使这些方法成为synchronized方法有两个效果:

  第一,不可能使两个调用synchronized的方法中有相同的对象出现交叉的现象。当一个线程作为一个对象正在执行synchronized的方法的时候,其他的线程请求执行同一个对象的synchronized方法的时候会出现挂起知道第一个线程被这个对象执行完毕。
  第二,当一个synchronized方法存在的时候,它会自动的生成一个事先的关系在请求一个后来的这个对象的synchronized方法.这保证了这种改变保证其他所有线程都是可见的。
  注意构造函数不能用作synchronized方法,对构造函数添加synchronized关键字是一种语法错误。构造函数的同步是讲不通的,因为当它正在被构造的时候,只有线程创建对象的时候才应该进入它。
  警告:当构造一个将会被多个线程共享的对象的时候,注意对一种对象的引用不存在过早的泄露。例如,假如你想维护一个包好每个实例的类的链表调用的实例。你应该增加临时的一行在下面的构造函数中。
  instances.add(this);
  但是,其他的线程可以在这个对象的构造完成之前使用这个实例。
  Synchronized方法是一种简单的策略能够组织线程冲突和内存一致性错误:假如一个对象能够被其他的线程可以看见,所有的读和写的变量都通过synchronized方法处理。(一个重要的异常:常量域,能够被一个已经生成构造函数的对象所修改)这种策略是有效的,但是,能够引起其他问题出现,我们在这个课程的以后可能会看到。

 养眼是必须滴^^

【翻译九】java-同步方法

你可能感兴趣的:(java)