The Java language keyword that, when used to decorate a method or a block of code, guarantees that at most one thread executes the code at the same time.
First , when two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed within a single time. The other thread must wait for the current thread to finish executing the block before it can execute the code block.
Second, however, when a thread accesses one synchronized (this) of an object to synchronize a block of code, another thread can still access the non- synchronized (this) synchronous code block in the object.
Third, in particular, when a thread accesses one synchronized (this) of an object to synchronize a block of code, other threads will block access to all other synchronized (this) synchronization blocks in object.
The third example also applies to other synchronous blocks of code. That is, when a thread accesses a synchronized (this) of object to synchronize a block of code, it obtains the object lock of the objects. As a result, other threads are temporarily blocking access to all of the synchronization code portions of the object.
The above rules apply to other object locks as well.
For various examples, see https://git.oschina.net/wenjieyatou/threadTest.
in concurrent programming, multi-threaded concurrent access resources are called critical resources, when multiple threads concurrently access the object and require the operation of the same resources, split the atomic operation is likely to occur inconsistent data or incomplete data situation, To avoid this, we take a synchronization mechanism to ensure that only one thread is allowed in a method at a time.
The synchronization mechanism implemented with the synchronized modifier is called the mutex mechanism, and the lock it obtains is called the mutex. Each object has a monitor (lock tag) that accesses the resource when the thread has the lock tag and enters the lock pool without a lock tag. Any object system creates a mutex for it, which is assigned to the thread to prevent an atomic operation from being interrupted. A lock on each object can be assigned to only one thread, so it is called a mutex.
here are some explanations for the use of the synchronization mechanism to obtain the mutex lock:
1. If there are two or more threads in the same method, each thread has its own copy of the local variables.
2 . Each instance of the class has its own object-level lock. When a thread accesses a synchronized synchronous code block or synchronous method in an instance object, the thread acquires an object-level lock on the instance, and other threads need to block the wait if they want to access the synchronized synchronous code block or synchronous method. The object-level lock is freed until the previous thread exits from a synchronous code block or method.
3. Access to synchronous code blocks in different instance objects of the same class, there is no blocking waiting to acquire object locks, because they get the object-level locks of their respective instances without affecting each other.
4 . Holding an object-level lock does not prevent the thread from being swapped out, nor does it block other threads from accessing non-synchronized code in the same sample object. When a thread a holds an object-level lock (that is, a code block or method that enters the synchronized adornment), the thread may also be swapped out, at which point the thread B may get the time to execute the code in that object, but it can only execute the non-synchronous code (not decorated with synchronized). When executing to the synchronization code, it will be blocked, at this point, the thread planner may also let a thread run, a thread continues to hold an object-level lock, when a thread exits the synchronization code (that is, the object-level lock is freed), if the B-thread runs again at this point, it will get the object-level lock. This executes the code in the synchronized.
5 . A thread holding an object-level lock will cause other threads to block out of all synchronized code. For example, there are three synchronized methods in a class a,b,c, when thread A is executing method A in an instance object m, it obtains that object-level lock, and other threads execute the code in the same instance object (that is, Object m). will be blocked at all synchronized methods, that is, at the method A,b,c is blocked, and so on when thread a frees the object level lock, other threads can go to the method, B or C code, to obtain the object-level lock.
6 . Using the synchronized (OBJ) synchronization statement block, you can obtain an object-level lock on the specified object. obj is a reference to an object, and if an object-level lock on the Obj object is obtained, when the Obj object is concurrently accessed, the wait is blocked at its synchronized code until the object-level lock to the Obj object is obtained. When obj is this, it gets the object-level lock of the current object.
7. Class-level locks are shared by all examples of specific classes and are used to control concurrent access to static member variables and static methods. The specific usage is similar to the object level lock.
8, mutual exclusion is a means to achieve synchronization, the critical area, mutual exclusion and signal volume are the main mutually exclusive implementation mode. After compiling the Synchronized keyword, the two bytecode instructions for Monitorenter and Monitorexit are formed before and after the synchronization block. According to the requirements of the virtual machine specification, in the execution of the monitorenter instruction, the first attempt to obtain the object's lock, if the lock is obtained, the lock counter is added 1, corresponding to the execution of the monitorexit instruction will reduce the lock counter 1, when the counter is 0 o'clock, the lock is released. Since the synchronized synchronization block is reentrant to the same thread, one thread can obtain the mutex of the same object multiple times, and the same, the corresponding number of times the mutex must be released in order to finally release the lock.
Java concurrency Programming (vii) synchronized detailed