is a lock operation optimization means, His core idea is: if a thread obtains a lock, then enter the bias mode, when the thread again request the lock, do not have to do any synchronous operation, so in the case of almost no lock competition, biased lock is a better optimization effect, Because it is most likely that the same thread requests the same lock multiple times in a row, it does not work well in the case where the lock is more competitive, because the most likely scenario is that each time a different thread requests the same lock, the bias mode is invalidated and the Java Virtual machine parameter-xx:+ Usebiasedlocking can turn on the bias lock,
If the bias lock fails, the virtual machine does not immediately suspend the thread, he will also use an optimization called lightweight lock, The operation of the lightweight lock is also very convenient, it simply takes the object head as a pointer to the thread holding the lock inside the stack to determine whether a thread holds the object lock, if the thread gets a lightweight lock succeeds, Can enter the critical section smoothly, if the lightweight lock locking fails, it means that the other thread to fight for the lock, then when the front-line lock request will be inflated to a heavyweight lock
After the lock expands, the virtual machine in order to avoid the thread true at the operating system level hangs, the virtual machine also will make the final effort, the spin lock, because the current thread temporarily cannot obtain the lock, but when can obtain the lock is an unknown, perhaps several CPU clock cycles, can get the lock, if so, Simply brutally suspending a thread can be an operation that is not worth the candle, so the system will make a bet, he will assume that in the near future, the thread can get the lock, so the virtual opportunity to make a few empty loops of the current thread, after a number of cycles, if the lock can be obtained, then smoothly into the critical section, If the lock is still not in place, the thread is actually suspended at the operating system level,
Lock elimination is a more thorough lock optimization, when the Java Virtual machine is JIT compiled, through the scanning of the running context, removing the lock that cannot compete with the shared resources, through the lock elimination, can save the time of meaningless request lock, if there is no competition, why should lock? This is because in the Java software development process, We will certainly use some of the Jdk's built-in api, such as stringbuffer,vector, when you use, you may not even consider how these objects are implemented internally, but in the real world, there is no competition, If the virtual machine detects this situation, it will remove these useless lock operations,
public string[] createstrings () { Vectornew vector<>(); for (int i = 0; i < i++) { v.add (integer.tostring (i)); } return V.toarray (new string[]{});
Lock elimination involves a key technique for escape analysis, so-called escape analysis is to see if a variable will escape a certain scope, in this case, the variable v obviously did not escape out of the Createstrings () method, based on this, the virtual machine can be boldly to the internal v lock Operation removal, The escape analysis must be performed in-server mode and the escape analysis can be opened using the-xx:+doescapeanalysis parameter. use the-xx:+eliminatelocks parameter to turn on lock Elimination.
The effort of the Java Virtual machine to lock optimization (reading notes)