Synchronized static methods on: such as: synchronized public static void the printa () {. . . } Plus plus keyword synchronized on a static and non-static method is fundamentally different method: applied to a class's static method is locked, non-static method is applied to the object lock such as: public class the Model { synchronized public static void the printa () { // get here to class lock lock System.out.println ( "thread name:" + . Thread.currentThread () getName ()); the try { the Thread.sleep ( 3000 ); the System .out.println ( "thread name:" +Thread.currentThread () getName ());. } The catch (InterruptedException E) { e.printStackTrace (); } } the synchronized public static void printb () { // get here to class lock lock System.out.println ( "thread name:" + Thread.currentThread () getName ());. the try { the Thread.sleep ( 2000 ); System.out.println ( "thread name:" + Thread.currentThread (). getName ()); } catch (InterruptedException E) { e.printStackTrace (); } } the synchronized public void printc () { // Lock the object here is to get a lock System.out.println ( "Thread Name:" + . Thread.currentThread () getName ()); // thread.sleep (2000); System.out.println ( "thread name:" + . Thread.currentThread () getName ()); } } Create three threads public class Thread1 the extends the thread { Private the Model Model; public Thread1 (the Model Model) { the this.model = model; } @Override public void run() { super.run(); Model.printa(); } } public class Thread2 extends Thread { private Model model; public Thread2(Model model) { this.model = model; } @Override public void run() { super.run(); model.printb(); } } public class Thread3 extends Thread { private Model model; public Thread3(Model model) { this.model = model; } @Override public void run() { super.run(); model.printc(); } } 运行一下代码: Model model = new Model(); Thread1 thread1 = new new Thread1(model); thread1.setName ( "A" ); thread1.start (); Thread2 Thread2 = new new Thread2 (Model); thread2.setName ( "B" ); thread2.start (); Thread3 thread3 = new new Thread3 (Model ); thread3.setName ( "C" ); thread3.start (); result: name of the thread: a thread name: C thread name: C thread name: a name of the thread: B name of the thread: b results of the analysis: a asynchronous and c, a and b synchronization. The reason: ab get the same lock: class lock, c got the object lock synchronized( Class ) to get synchronized with the lock on the static method using the same as will now be printc model was changed to the above methods: public void printc () { synchronized (the Model. Class ) { // At this time, to get a lock for the class lock, with the other two methods is the synchronization method System.out.println ( "thread name:" + Thread.currentThread () getName ().); the try { the Thread.sleep ( 2000 ); } the catch (InterruptedException E) { E .printStackTrace (); } System.out.println ( "thread name:" + . Thread.currentThread () getName ()); } } Running the above code again, the results: Thread name: a name of the thread: a thread name: C thread Name: C thread Name: B thread Name: B From the results, a method of synchronizing three Note, if the string is used as lock object, the same string lock objects of the same use of the volatile keyword: the main role is to make the variables between multiple threads again visible principle: volatile variable is modified later, get the value of this variable, when forces from the common stack We get the value of a variable, rather than from the thread to obtain the values of variables private data stack thread-safety issues volatile: the thread safety and visibility of atoms comprising two aspects, volatile to ensure the visibility between threads, but can not guarantee between threads atomic added: three characteristics multithreading: atomicity, visibility, orderly atomic: is a operation is not interrupted. Even when executed with multiple threads, an action once started, will not be interference from other threads. For example, for a static global variable int i, assign the value two threads, thread A assigned to him 1, thread B assigned to him -1 in what way. What pace of work, the value of i is either 1, either. No matter two threads -1 . Between thread A and thread B are without interference. This is a feature of atomicity, it can not be interrupted. Visibility: refers to when a thread modifies the value of a variable of a shared, other threads can immediately know whether this modification. Obviously, for the serial, the visibility of the problem does not exist. Ordering: When concurrent execution of the program might appear out of order. It gives the visual impression is: EDITORIAL code will be executed later. The reason is because the order of the problem in the program execution, instructions may be rearranged, rearranged instruction does not necessarily coincide with the order of the original instruction. First, the common thread's stack and proprietary stacks when you start a thread, the value of the variable is present in the public private stack stacks and thread. It is set in the JVM - for efficiency threads running when the server mode thread has been taken in the private stack variable's value, even if there are other threads value of the variable has been modified, updated is a variable value of the common stack, private stack value will not change, which leads to a problem running thread