Java Foundation---Multithreading---synchronized implementation principle

Source: Internet
Author: User

The synchronized is used in three places. Modifies the normal method, then the thread needs to acquire the object lock. Modifies the static method, then the thread needs to acquire the class lock, which is actually the class object. The class lock and object lock are not interfering with each other. Modify the code block, and also get the object lock. When used for code blocks, corresponding to the Monitorenter directive and the MONITOREXIT directive, indicating that a monitor lock is required to execute this block of code for the method, there is a acc_synchronized indicator in the flag parameter of the method area method. Indicates that the thread wants to execute this method before it must obtain a monitor lock.   For monitor locks, each object has a monitor lock. This lock in the hotspot virtual machine is a Objectmonitor object data structure implemented in C + +, which maintains two queue synchronization queues and waiting queues, a bit like the two queues of Aqs. When multiple threads are acquiring the lock at the same time, they enter the synchronization queue, pointing to the thread that acquires the lock through a pointer, and a counter count is used to do the lock re-entry. The thread enters the wait queue when the wait method is called.  blog.csdn.net/yinbucheng/article/details/72524482 to understand synchronized needs to understand the concept of object headers. What three pieces of data does the ===java object store in memory? What two messages are stored in the object header? Includes object header, instance data, aligned padding object header The most important two parts: Mark Word, type pointer 1.mark there is a common section in Word that includes the HASHCODE,GC generational age, and the lock status code represents several lock states of an object. As the lock state is different, Mark Word presents a different structure. When the lock is biased, it is the thread ID that gets the bias lock, which is the pointer to the line Cheng record. For example, a heavyweight lock has a pointer to the object lock monitor, and the object lock monitor is a data structure implemented by C + +.  2. Type pointer to determine what class an object is an instance of. What are the lock states of   ? Synchronized introduces lock escalation and different lock states in order to reduce the performance cost of acquiring and releasing locks = = = What is the status of Mark Word in the object header? What is the state of the lock that corresponds to the lock status code? The lock state has 4---no lock (whether it is a biased lock flag of 0), a bias Lock: Mark Word holds the id00---lightweight lock of the thread that acquires the lock, holding a pointer to the line Cheng record. With the---Heavyweight lock, the pointer lock that holds the monitor object can be continuously upgraded and cannot be turned back.   Lock escalation Process = = = Bias Lock applicable conditions? For situations where lock contention does not exist, the same thread acquires the lock repeatedly. When this thread acquires a biased lock, Mark Word records the ID of the thread so that the same thread acquires the lock without any processing, and does not need to use CAs to acquire or release the lock. But when there is a lock competition, other threads also want the lock to become a lightweight lock, and the structure of the Markword is changed.  === what are the conditions for lightweight locks? Applies to the thread alternately executes the synchronous code block situation, also does not exist the lock competition premise. Lightweight locks have pointers to the stacks lock record area of the wire. The lock record lock record area is created in the stack frame of the thread, and a pointer to mark Word is also pointed to the locks recording area. They point to each other. If you fail to acquire a lightweight lock, you will attempt to use a spin lock.  === Adaptive spin lock? The thread acquires a lightweight lock in the process of failure, in order to avoid the first failure to suspend, threads will spin a certain number of attempts to obtain again. Upgrade to a heavyweight lock if you still cannot get to the lock.  === lock Coarsening (lock coarsening) when it comes to a thread that repeatedly locks and unlocks the same object, it merges the process into a wider range of lock and unlock operations. 6 public void Append () {7 stringbuffer.append ("a"), 8 stringbuffer.append ("B"), 9 Stringbuffer.append ("C"); 10} Locks are added at the first append, and the locks are released at the last append.  === Lock elimination applicable scenario? Disable the JVM directive for lock elimination? Reduce some unnecessary locking. It is generally involved in the method of local variables can be lock-eliminated. For example, create local variables within a method StringBuilder loop 10,000 times append a character, the append itself is to be locked but this way because it is a local variable other threads can not touch to lock elimination. for (int i = 0; i < 100000000; i++) {test02.append ("abc", "Def"),}public void Append (String str1, String str2) {1 9 StringBuffer sb = new StringBuffer (), Sb.append (STR1). Append (str2);}                   

Java Foundation---Multithreading---synchronized implementation principle

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.