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