Java bias Lock

Source: Internet
Author: User

Overview

The bias lock is a lock optimization method proposed by JDK 1.6, and the core idea is that if the program does not compete, the synchronization of the thread that has already taken the lock is canceled. In other words, when a lock is acquired by a thread, it enters the biased lock mode, and when the thread requests the lock again, there is no need to synchronize the operation, thus saving the operation time. However, if there are other threads requesting this lock during this time, exit the bias lock mode. A biased lock can be enabled in the JVM using the-xx:+usebiasedlocking setting.

Experiment

Look at the following code:

 PackageCom.winwill.lock;ImportJava.util.List;ImportJava.util.Vector;/** * @author Qifuguang * @date 15/6/5 13:44 * * Public  class testbiasedlock {    Private Staticlist<integer> list =NewVector<integer> (); Public Static void Main(string[] args) {LongTsstart = System.currenttimemillis (); for(inti =0; I <10000000;        i++) {list.add (i); } System.out.println ("Total Cost:"+ (System.currenttimemillis ()-Tsstart) +"MS"); }}

The code uses a loop to add elements to the vector, and the vector's add operation requests the Lock:

    /**     * Appends the specified element to the end of this Vector.     *     * @param e element to be appended to this Vector     * @return {@code true} (as specified by {@link Collection#add})     * @since 1.2     */    publicsynchronizedbooleanadd(E e) {        modCount++;        1);        elementData[elementCount++] = e;        returntrue;    }

We run the code using the following parameters:

-xx:+usebiasedlocking
-xx:biasedlockingstartupdelay=0
-xmx512m
-xms512m

-xx:biasedlockingstartupdelay=0 This parameter indicates that the virtual machine starts the biased lock mode when it starts, because by default, the virtual machine starts with a 4s bias lock mode, which is a short running time, so this setting is done.

The following results are obtained:

However, if we run the program using the following parameters:

-xx:-usebiasedlocking
-xmx512m
-xms512m

The results are as follows:

Conclusion

Biased locking in the case of less competition, the system can help.

Precautions

Biased lock in the case of intense competition is not too strong optimization effect, because a lot of competition will cause the lock thread to keep switching, lock also difficult to maintain the bias mode, at this time, using a biased lock not only can not optimize the program, but it is possible to reduce program performance. Because, in a highly competitive scenario, a biased lock can be disabled using the-xx:-usebiasedlocking parameter.

Java 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.