---
title: "Java synchronized methods"
author: "standalone"
date: "2010-08-31T12:00:49Z"
tags: []
categories: ["编程语言"]
slug: "java-synchronized-methods"
draft: false
lastmod: "2010-08-31T12:00:49Z"
id: 751584
url: "https://standalone.iteye.com/blog/751584"
---

- 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方法执行完后，保证了对这个对象做的所有改动都对其他线程可见。