What is object orientation? If for just graduated out as it is encapsulation, inheritance, polymorphism, why do I say so, because at that time I will only say so, I now to answer, object-oriented is a kind of thought, also is the object of all things, sound very abstract, yes, he is abstract, thought if not abstract, that he must be not flexible, Abstract thought also corresponds to the abstract interface, object-oriented development mode, is to use the abstract interface to the front of the universal, so as to stay away from many if else principle after the expected side of the code.

After talking for a long time, synchronized, the protagonist of today, which is a word I always assign value to paste, after all, too long, if object oriented is a world, then he is every world can not acquire things, lock.

1 He has those usage scenarios

This is needed whenever high concurrency occurs, such as generating a unique order number.

2 What is the implementation principle?

He does it by manipulating objects using the JVM. How does the JVM manipulate objects? Let’s start by looking at what objects are made of

public class ObjectContent {
    // -XX:-UseCompressedOops
    public static void main(String[] args) {
        System.out.println(VM.current().details());
        Object obj = new Object(a);// Get the binary string representation of type int
        System.out.println(Integer.toBinaryString(obj.hashCode()));
        // Gets a hexadecimal string representation of type int
        System.out.println(obj + "Hex hash:"+ Integer.toHexString(obj.hashCode())); System.out.println(ClassLayout.parseInstance(obj).toPrintable()); }}Copy the code

The output

java.lang.Object object internals:

OFFSET SIZE TYPE DESCRIPTION VALUE

  0     4        (object header)                           01 6f 73 ac (00000001 01101111 01110011 10101100) (-1401721087)

  4     4        (object header)                           0c 00 00 00 (00001100 00000000 00000000 00000000) (12)

  8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)

 12     4        (loss due to the next object alignment)
Copy the code

Instance size: 16 bytes

It can be seen that the object consists of Header instance data and its filling

The object header consists of a mark-word field and a Klass Pointer to a Class object

The mark information for the lock is in Mark-Word

Object (16 bytes); mark-word (8 Klass Pointers)

The Mark-word contains Monitor objects

Monitor has three different implementations that are three different locks, and when you’re multithreading you’re competing for that object

Synchronized acquires locks this way

2 Lock upgrade process (biased lock, lightweight lock, heavyweight lock)

When the synchronized keyword is added, if there is a competition, it is a biased lock, and then another thread comes to compete, which is a lightweight lock. At this time, many threads come to compete, and these threads will spin first. After about ten spins, if there is no resource, they will upgrade to a heavyweight lock

3 Is this a JVM level lock or a JDK level lock

It is a JVM-level lock because it is assigned to the JVM

4 Is he an optimistic or pessimistic lock

He is pessimistic lock

Pessimistic Lock:

Every time we acquire data, we worry that the data will be modified, so we lock the data every time we acquire data to ensure that the data will not be modified by others in the process of our own use, and unlock the data after use. Because the data is locked, other threads that read or write to the data wait.

Optimistic Lock:

Every time the data is acquired, the data will not be modified. Therefore, the lock will not be applied every time the data is acquired. However, when the data is updated, it is necessary to determine whether the data has been modified by others. If the data is modified by another thread, the data is not updated. If the data is not modified by another thread, the data is updated. Because the data is not locked, it can be read and written by other threads.