Java high concurrency synchronized
Synchronized can ensure that when a code block or method is running, only one method can enter the critical area at the same time, and it can also ensure the memory visibility of shared variables.
synchronized can be used in three situations:
1. Ordinary synchronization method, the lock is the current instance object
2. Static synchronization method, the lock is the class object of the current class
3. Synchronized method block, lock is the object inside the brackets
package com.tianmaying.crawler.impl; public class SynchronizedTest { public synchronized void test1(){} }
public void test2(){
synchronized (this){}
}
public static synchronized void test3(){}
Decompile the project with javap -c SynchronizedTest
It can be seen that:
Synchronized method, synchronized static method: all methods are marked as synchronized, and there is no special mark, but according to the article " http://www.cnblogs.com/javaminer/p/3889023.html ", it indicates that the access_flags field of the method The synchronized flag position is 1
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Two concepts: java object header, monitor