What optimizations have been made for locks in the Java Virtual machine?

Java multi-thread this also is a bottomless pit, how to learn is not over, so now a lot of the book are introduced multi-threaded, there are quite a few in my hands, such as the Java high concurrent programming, rounding, the Java multi-thread programming practical guide, concurrent Java programming of actual combat, the Jva multithreaded design patterns, and a highly recommended, Is “deep understanding of parallel programming”, this is a Linux kernel source code to do the explanation, which in-depth introduction to the memory barrier and other knowledge, there are most really do not understand.

If all the contention is handled by the operating system, the performance is very low. Therefore, the virtual machine needs to solve the contention in the virtual machine layer before the operating system suspends the thread. It doesn’t need to be handled by the operating system boss. Threads in Java correspond to threads in the operating system. When a thread is started in Java, the corresponding operating system calls pthread_CREATE (Linux) to start a thread.

According to all kinds of books, don’t know from that version to start (a few books record, some say 1.5, 1.6, some said I didn’t also in depth), the HotSpot development team has spent a lot of energy to achieve a variety of lock optimization techniques, such as elimination lock, lock coarsening, lightweight lock, biased locking, these technologies are to deal with the problem of competition in multithreaded programming.

Each object in the Java VIRTUAL machine has an object header, which is used to store the information of the object. There is a part called Mark Word, which is very important. It stores the hash value of the object, the age of the object, the pointer to the lock and other information, which is the key to achieve the lock.

32-bit object headers are as follows:

The 64-bit object header is as follows:

For example, in a 32-bit system, Mark Word is 4 bytes, in which 25 bits represent the hashCode of the object, 4 bits represent the age of the object, 1 bit represents whether the bit is biased to lock, 2 bits represent the lock information, 64 bits are 8 bytes, in which 31 bits are the hashCode of the object, and 2 bits represent the marker of the lock status. If you want to see a finer structure of Java objects, you can use the JOL tool.

We won’t go into details here, but this chapter is about biased locking.

Will performance improve after bias locking is disabled?

Biased locking is 1.6 to add a lock optimization technique, the purpose is to solve the problem of data synchronization in the absence of any competition, “partial” keyword is to first get his thread, if other threads for follow-up, so hold biased locking threads will not be in sync, virtual opportunity of a head of the sign bit is set to “01”, on behalf of the bias model, In addition, CAS is used to record the id of the thread that has acquired the lock in Mark Word. When another thread acquires the lock, the biased lock mode will end.

However, biased locking is not always beneficial. If the lock in the program is always accessed by different threads, biased locking is unnecessary, because a large number of competition will lead to the continuous switching of the thread holding the lock. In this case, biased locking cannot be improved, but may reduce the system performance, which needs to be prohibited by -xx: -usebiasedlocking.

Vector lock (synchronized); Vector lock (synchronized); Vector lock (synchronized);

public class BasicLockMain {
    private static Vector<Integer> vector =new Vector<>();
    public static void main(String[] args) {
        long startTimer =System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) { vector.add(i); } System.out.println(System.currentTimeMillis()-startTimer); }}Copy the code

On my older computer, it takes at least 2291 milliseconds to complete 10 runs, so let’s add some parameters.

-XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0
Copy the code

The running results at least 10 times is 2080 milliseconds, though not many, but also has improved, parameters – XX: BiasedLockingStartupDelay = 0 is used in the virtual machine starts, enable biased locking immediately, if you don’t set this parameter, the default virtual after 4 seconds, to launch a biased locking.

conclusion

Therefore, biased locks are only suitable for use in systems where most locks are not contested. If there are a large number of contested locks in the system, you can consider turning off biased locks. Lightweight locks are discussed in the next chapter.