As the name implies is used to synchronize the role of mutual exclusion.
Here's a concise note of how it's used and what it means:
1. When synchronized modifies This or a non-static method or An instance , the synchronized lock is added to this or the instance object reference. For example, a and a are instantiated objects for the main class, a calls the method that is synchronized, and B calls the method that is synchronized, and does not form a mutex. However, the method that the A-object call of different threads is synchronized is mutually exclusive.
Public synchronized void Method () {//...} public void Method () {synchronized (this) {//..... } }
2. Unlike 1, when synchronized modifies the class name. class or static method . Even if they are different objects, they will form mutual exclusion.
Class Main {public synchronized static void Method1 () {//...} public void Method2 () {synchronized (Main.class)//}}
Transfer from http://blog.csdn.net/cq361106306/article/details/38736551
Java Foundation-synchronized usage of keywords