Common locks in Java thread concurrency-spin lock bias Lock

Source: Internet
Author: User

With the development of the Internet, more and more Internet enterprises are confronted with the concurrency security problems caused by the expansion of user volume. This paper mainly introduces several kinds of locking mechanisms common in Java concurrency.

1. Bias Lock

 

The bias lock is a mechanism of lock optimization proposed by JDK1.6. The core idea is that if the program does not compete, it cancels the thread synchronization operation that has acquired the lock before. In other words, if a lock is acquired by the thread, it enters the bias mode, and when the thread requests the lock again, it does not need to synchronize the operation, thus saving the operation time, if there are other threads to lock the request, then the lock exits the bias mode. Using-xx:+usebiasedlocking in the JVM

Package Jvmproject;import Java.util.list;import Java.util.vector;public class biased {public    static list< integer> numberlist = new vector<integer> ();    public static void Main (string[] args) {        long begin = System.currenttimemillis ();        int count = 0;        int startnum = 0;        while (count<10000000) {            numberlist.add (startnum);            startnum+=2;            count++;                }        Long end = System.currenttimemillis ();        System.out.println (End-begin);    }}

Initializes a vector, adds 10 million integer objects to it, and then outputs the time difference. This is to test the performance of the biased lock. Why use vectors instead of ArrayList?

Because ArrayList is thread insecure, vectors are thread-safe. This may not be enough specific, you can look over the source bar.

Almost all of the operations in the vector are sychronized, and ArrayList is not, so the vectors are thread-safe.

Next, let's test how much of a program's performance is affected by turning on a biased lock and not turning on the bias lock.

Configure the JVM startup (turn on bias Lock) parameter:

Configure the JVM startup (turn off bias lock) parameter:

perfect! Turn on the biased lock program running time is significantly shorter, open the bias lock ratio does not open the biased lock, in a single thread operation of an object synchronization method, there is a certain advantage. It is also possible to understand that when only one thread operates a vector object with a synchronous method, the operation of the vector is transformed into a ArrayList operation.

Biased lock in the lock competition is not too strong optimization effect, because a lot of competition will cause the lock thread to keep switching, the lock is difficult to maintain in the bias mode, the use of biased lock not only not get the performance optimization, but may reduce the performance of the system, so in the case of fierce competition, you can try to use

The-xx:-usebiastedlocking parameter disables the biased lock.

2. Lightweight lock

If the bias lock fails, the Java virtual machine will ask the thread to request a lightweight lock, a lightweight lock inside the virtual machine, implemented using an object that becomes basicobjectlock, which consists of a Basiclock object and a Java object pointer holding the lock. The Basicobjectlock object is placed in the Java stack frame. The Displaced_header field is also maintained inside the Basiclock object, which is used to back up Mark Word, the object's head.

When a thread holds a lock on an object, the object's head mark Word information is as follows

[PTR |00] Locked

The end of the two-bit bit is 00, and the entire mark Word is a pointer to the Basiclock object. Because the Basicobjectlock object is in the thread stack, the pointer necessarily points to the line stacks space holding the lock. When it is necessary to determine whether a thread holds the object, simply determine whether the pointer to the object header is in the stack address range of the current thread. At the same time, the Displaced_header of the Basiclock object is backed up with the original object's Mark Word content, and the obj field of the Basicobjectlock object points to the head of the object holding the lock.

3. Heavy-Lock

When a lightweight lock fails, the virtual machine uses a heavy-level lock. When using a heavyweight lock, the object's Mark Word is as follows:

[PTR |10] Monitor

Heavy lock during operation, threads may be suspended at the operating system level, and if so, the cost of switching and calling between threads is greatly increased.

4. Spin Lock

A spin lock allows a thread to not be suspended while not acquiring a lock, and to go to an empty loop (known as spin, which executes an empty loop), if the thread can acquire a lock after several empty loops, continue execution. If the thread still cannot get the lock, it will be suspended.

With a spin lock, the chance of a thread being suspended is relatively reduced, and thread execution is relatively consistent. Therefore, for those lock competition is not very intense, the lock occupies a short time of concurrent threads, has certain positive meaning, but for the lock competition intense, the single-threaded lock takes a long time concurrent program, the spin locks in the spin waits, often resolutely cannot obtain the corresponding lock, not only wasted CPU time, In the end, it is unavoidable to suspend the operation, instead of wasting the resources of the system.

In JDK1.6, the Java Virtual machine provides the-xx:+usespinning parameter to turn on the spin lock, using the-xx:preblockspin parameter to set the number of spin-lock waits.

At the beginning of the JDK1.7, the parameters of the spin lock are canceled, the virtual machine is no longer supported by the user to configure the spin lock, the spin lock is always executed, and the spin lock number is automatically adjusted by the virtual machine.

Http://www.cnblogs.com/think-in-java/p/5520462.html

Common locks in Java thread concurrency-spin lock bias Lock

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.