The effort of the Java virtual machine to lock optimization

Source: Internet
Author: User

As a common platform, the JDK itself is also the performance of concurrent programs, and in the JDK do everything possible to provide the concurrency of the system throughput. here, I'll give you a brief introduction to several "lock" optimization strategies within the JDK.

1. Lock bias

Lock bias is an optimization method for locking operation.

If a thread gets a lock, the lock goes into biased mode. When the thread requests the lock again, no more synchronization is required. This saves a lot of operation on the lock request, which improves program performance.

Therefore, for the situation of almost no lock competition, the bias lock has a more red-ah optimization effect, because many times in a row is most likely to be the same thread request the same lock. But for the lock competition more intense occasions, its effect is poor. Because in highly competitive situations, the most likely scenario is that each time a different thread requests the same lock. Click here to learn about several common locks.

2. Lightweight lock

If the bias lock fails, the last requested lock thread is not the same thread as this one. A bias lock failure means that a synchronous operation cannot be avoided. at this point, the virtual machine does not immediately suspend the thread. He will use an optimization method that becomes a lightweight lock.

The lightweight lock operation is also convenient, it simply takes the object's head as a pointer, referring to the inside of the thread stack of the Shang Yuyu lock, to determine whether a thread holds an object lock. If the thread obtains a lightweight lock successfully, it can enter the critical section smoothly. If a lightweight lock fails, it means that other threads preempt the lock, then the lock request of the front thread expands to a heavyweight lock. Click here to learn about several common locks.

3. Optional lock

after the lock expands, the virtual machine tries to prevent the thread from actually suspending at the operating system level, and the virtual machine is making a final effort – an optional lock. since the current thread is temporarily unable to acquire a lock, it is unknown when the lock can be obtained. Perhaps after several clock cycles of the CPU, the lock can be obtained. If this is the case, a simple and rude suspend thread can be an operation that is not worth the candle, so the system will make a bet: it will assume that in the near future, the thread will get the lock.

Therefore, the virtual machine lets the current thread make an empty loop, after several cycles, if the lock can be obtained, then smoothly into the critical section. If the lock is not available, the thread is actually suspended at the operating system level.

4, Lock elimination

Lock elimination is a more thorough lock optimization. When the Java Virtual machine is JIT compiled, it removes the lock that cannot compete with the shared resource by scanning the running context. With lock elimination, you can save meaningless request lock times.

In this case, we use vectors, and the vector internally uses the Synchronize request lock.

Public String [] createstrings () {
vector<string> v= new vector<string> ();
for (int i=0;i<100;i++) {
V.add (integer.tostring (i));
}
Return V.toarray (New string[]{});
}

Since V is only used in function createstrnigs, it is simply a local variable. Local variables are allocated on the thread stack and are part of the threads ' private data and therefore cannot be accessed by other threads. Therefore, in this case, all the lock synchronization inside the vector is not necessary. If the virtual machine detects this situation, it will remove these useless lock operations. Click here to learn about several common locks.

lock removal involves a key technique for escape analysis. The so-called escape analysis is to see if a variable will escape a certain scope. in this case, the variable v obviously does not escape from the createstring function. Based on this, the virtual machine can boldly remove the lock operation inside the V. If Createstrings returns not a string array, but v itself, then it is assumed that the variable v escapes the current function, which means that V is likely to be accessed by other threads. If so, the virtual machine cannot eliminate the lock operation in V.

Escape analysis must be performed in-server mode, you can open escape analysis using the-xx:doescapeanalysis parameter, and you can use the-xx:+eliminatelocks parameter to open lock elimination.

This digest is from "Java High concurrency Program design" a book.

The effort of the Java virtual machine to lock optimization

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.