- 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方法执行完后,保证了对这个对象做的所有改动都对其他线程可见。
本文详细解析了synchronized方法的作用:一是确保同一对象的synchronized方法不会被多个线程并发执行;二是保证方法执行后对象状态的变化对其他线程可见。
1123

被折叠的 条评论
为什么被折叠?



