Java synchronized methods

 

 

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

在sun的tutorial上,对synchronized methods有上面的解释。意思是一个method加了这个关键字之后, 第一,多个线程不能同时执行同一个对象的synchronized方法,只能串行。第二,在一个synchronized方法执行完后,保证了对这个对象做的所有改动都对其他线程可见。

 

 

 

你可能感兴趣的:(java,thread,sun)