"Java Concurrency Programming" VII: A few notes on using synchronized to obtain mutex locks

Source: Internet
Author: User

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 there may be inconsistent data or incomplete data, in order to avoid the occurrence of this situation, We will take a synchronization mechanism to ensure that only one thread is allowed within a method at a certain point in 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. However, it can only execute non-synchronous code (not decorated with synchronized), when executed to the synchronization code, it will be blocked, 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 release of the object-level lock), if the B-thread is run again , the object-level lock is obtained, which 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: A few notes on using synchronized to obtain mutex locks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.