Java Lock Removal

Source: Internet
Author: User

Overview

Lock Elimination is a Java virtual machine that is JIT-compiled by scanning the running context, removing the lock that cannot compete with the shared resources, and eliminating the lock by locking, which saves meaningless request lock time.

Experiment

Look at the following code:

 PackageCom.winwill.lock;/** * @author Qifuguang * @date 15/6/5 14:11 * * Public  class testlockeliminate {     Public StaticStringgetString(string s1, string s2) {StringBuffer SB =NewStringBuffer ();        Sb.append (S1); Sb.append (S2);returnSb.tostring (); } Public Static void Main(string[] args) {LongTsstart = System.currenttimemillis (); for(inti =0; I <1000000; i++) {getString ("Testlockeliminate","Suffix"); } System.out.println ("Total Cost:"+ (System.currenttimemillis ()-Tsstart) +"MS"); }}

The number of StringBuffer in the GetString () method takes a local variable inside the function, and it is not possible to escape out of the method, so he cannot be accessed by multiple threads at the same time, and there is no competition for resources. However, the StringBuffer append operation requires a synchronous operation:

    @Override    publicsynchronizedappend(String str) {        null;        super.append(str);        returnthis;    }

Escape analysis and lock elimination can be switched on using the parameters-xx:+doescapeanalysis and-xx:+eliminatelocks (lock cancellation must be in-server mode). Run the above program using the following parameters:

-xx:+doescapeanalysis-xx:-eliminatelocks

The following results are obtained:

Run the program using the following command:

-xx:+doescapeanalysis-xx:+eliminatelocks

The following results are obtained:

Conclusion

As can be seen from the above example, the closure of the lock elimination, StringBuffer every time the append will be a lock application, waste unnecessary time, the unlock after the performance has been improved.

Java Lock Removal

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.