Home  >  Article  >  Java  >  What is the principle and process of Java Synchronized lock upgrade?

What is the principle and process of Java Synchronized lock upgrade?

WBOY
WBOYforward
2023-04-19 22:22:121608browse

Tool preparation

Before we formally talk about the principle of synchronized, let’s talk about Spin lock, because Spin lock plays a very important role in the optimization of synchronized. big effect. To understand Spin Lock, we first need to understand what atomicity is.

The so-calledatomicitySimply put, each operation is either not done or done all. Doing all means that it cannot be interrupted during the operation, such as variablesdataTo perform an increment operation, there are the following three steps:

  • Load data from memory to the register.

  • Add one to the value of data.

  • Write the result back to memory.

Atomicity means that when a thread performs an increment operation, it cannot be interrupted by other threads. Only when this thread completes these three processes can other threads be able to manipulate data. data.

Let’s experience it now with code. In Java, we can use AtomicInteger to perform atomic operations on integer data:

import java.util.concurrent.atomic.AtomicInteger;
 
public class AtomicDemo {
 
  public static void main(String[] args) throws InterruptedException {
    AtomicInteger data = new AtomicInteger();
    data.set(0); // 将数据初始化位0
    Thread t1 = new Thread(() -> {
      for (int i = 0; i < 100000; i++) {
        data.addAndGet(1); // 对数据 data 进行原子加1操作
      }
    });
    Thread t2 = new Thread(() -> {
      for (int i = 0; i < 100000; i++) {
        data.addAndGet(1);// 对数据 data 进行原子加1操作
      }
    });
    // 启动两个线程
    t1.start();
    t2.start();
    // 等待两个线程执行完成
    t1.join();
    t2.join();
    // 打印最终的结果
    System.out.println(data); // 200000
  }
}

From the above code analysis, we can know that, If it is a general integer variable, if two threads operate at the same time, the final result will be less than 200,000.

Let's now simulate the process of problems with general integer variables:

The initial value of main memory data is equal to 0, and the obtained by the two threads dataInitial values ​​are equal to 0.

What is the principle and process of Java Synchronized lock upgrade?

Now thread one adds one to data, and then thread one synchronizes the value of data back to the main memory. The entire memory The data changes are as follows:

What is the principle and process of Java Synchronized lock upgrade?

Now thread two data adds one, and then synchronizes the value of data back to the main memory (replace the original The value of the main memory is overwritten):

What is the principle and process of Java Synchronized lock upgrade?

We originally hoped that the value of data would become 2## after the above changes. #, but thread two overwrites our value, so in a multi-threaded situation, our final result will become smaller.

But in the above program, our final output result is equal to 20000. This is because the operation of

1 on data is atomic and indivisible. During the operation, other threads cannot operate on data. This is the advantage brought by atomicity.

In fact, the above

1 atomic operation is implemented through Spin lock. We can take a look at the source code of AtomicInteger:

public final int addAndGet(int delta) {
  // 在 AtomicInteger 内部有一个整型数据 value 用于存储具体的数值的
  // 这个 valueOffset 表示这个数据 value 在对象 this (也就是 AtomicInteger一个具体的对象)
  // 当中的内存偏移地址
  // delta 就是我们需要往 value 上加的值 在这里我们加上的是 1
  return unsafe.getAndAddInt(this, valueOffset, delta) + delta;
}

The above code is ultimately implemented by calling the method of the

UnSafe class. Let’s take a look at its source code again:

public final int getAndAddInt(Object o, long offset, int delta) {
  int v;
  do {
    v = getIntVolatile(o, offset); // 从对象 o 偏移地址为 offset 的位置取出数据 value ,也就是前面提到的存储整型数据的变量
  } while (!compareAndSwapInt(o, offset, v, v + delta));
  return v;
}

The main process of the above code is to continuously Get the data with the offset address

offset in the memory, and then execute the statement !compareAndSwapInt(o, offset, v, v delta)

The main function is to compare whether the data with memory offset address

offset of object o is equal to v. If it is equal to v, the offset will be The data at the address offset is set to v delta. If this statement is executed successfully, it returns true, otherwise it returns false. This is what we usually do. Talking about CAS in Java.

When you see this, you should have discovered that when the above statement fails to execute, the while loop operation will continue until the operation is successful. The while loop will not exit until the operation is successful. If the operation is not successful, it will continue to "spin". "Here, an operation like this is

spin, and the lock formed by this spin method is called spin lock.

Memory layout of objects

In the JVM, there are three main pieces of memory for a Java object:

  • Object header, which contains two parts of data , respectively

    Mark word and type pointer (Kclass pointer).

  • Instance data is the various data we define in the class.

  • Alignment padding, JVM requires that the memory size occupied by each object needs to be an integer multiple of 8 bytes during implementation. If the memory size occupied by the data of an object is not enough If it is an integer multiple of 8 bytes, it needs to be filled to 8 bytes. For example, if an object station is 60 bytes, it will eventually be filled to 64 bytes.

And closely related to the synchronized lock upgrade principle we are going to talk about is

Mark word. This field mainly stores data when the object is running, such as the object Hashcode, GC generation age, thread holding the lock, etc. The Kclass pointer is mainly used to point to the class of the object, mainly to indicate which class the object belongs to, and to find the metadata of the class.

In the 32-bit Java virtual machine, the Mark word has 4 bytes and a total of 32 bits. Its content is as follows:

What is the principle and process of Java Synchronized lock upgrade?

我们在使用synchronized时,如果我们是将synchronized用在同步代码块,我们需要一个锁对象。对于这个锁对象来说一开始还没有线程执行到同步代码块时,这个4个字节的内容如上图所示,其中有25个比特用来存储哈希值,4个比特用来存储垃圾回收的分代年龄(如果不了解可以跳过),剩下三个比特其中第一个用来表示当前的锁状态是否为偏向锁,最后的两个比特表示当前的锁是哪一种状态:

  • 如果最后三个比特是:001,则说明锁状态是没有锁。

  • 如果最后三个比特是:101,则说明锁状态是偏向锁。

  • 如果最后两个比特是:00, 则说明锁状态是轻量级锁。

  • 如果最后两个比特是:10, 则说明锁状态是重量级锁。

而synchronized锁升级的顺序是:无????->偏向????->轻量级????->重量级????。

在Java当中有一个JVM参数用于设置在JVM启动多少秒之后开启偏向锁(JDK6之后默认开启偏向锁,JVM默认启动4秒之后开启对象偏向锁,这个延迟时间叫做偏向延迟,你可以通过下面的参数进行控制):

//设置偏向延迟时间 只有经过这个时间只有对象锁才会有偏向锁这个状态
-XX:BiasedLockingStartupDelay=4
//禁止偏向锁
-XX:-UseBiasedLocking
//开启偏向锁
-XX:+UseBiasedLocking

我们可以用代码验证一下在无锁状态下,MarkWord的内容是什么:

import org.openjdk.jol.info.ClassLayout;
 
import java.util.concurrent.TimeUnit;
 
public class MarkWord {
 
  public Object o = new Object();
 
  public synchronized void demo() {
 
    synchronized (o) {
      System.out.println("synchronized代码块内");
      System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
  }
 
  public static void main(String[] args) throws InterruptedException {
    System.out.println("等待4s前");
    System.out.println(ClassLayout.parseInstance(new Object()).toPrintable());
    TimeUnit.SECONDS.sleep(4);
 
    MarkWord markWord = new MarkWord();
    System.out.println("等待4s后");
    System.out.println(ClassLayout.parseInstance(new Object()).toPrintable());
    Thread thread = new Thread(markWord::demo);
    thread.start();
    thread.join();
    System.out.println(ClassLayout.parseInstance(markWord.o).toPrintable());
 
  }
}

上面代码输出结果,下面的红框框住的表示是否是偏向锁和锁标志位(可能你会有疑问为什么是这个位置,不应该是最后3个比特位表示锁相关的状态吗,这个其实是数据表示的大小端问题,大家感兴趣可以去查一下,在这你只需知道红框三个比特就是用于表示是否为偏向锁锁的标志位):

What is the principle and process of Java Synchronized lock upgrade?

从上面的图当中我们可以分析得知在偏向延迟的时间之前,对象锁的状态还不会有偏向锁,因此对象头中的Markword当中锁状态是01,同时偏向锁状态是0,表示这个时候是无锁状态,但是在4秒之后偏向锁的状态已经变成1了,因此当前的锁状态是偏向锁,但是还没有线程占有他,这种状态也被称作匿名偏向,因为在上面的代码当中只有一个线程进入了synchronized同步代码块,因此可以使用偏向锁,因此在synchronized代码块当中打印的对象的锁状态也是偏向锁

上面的代码当中使用到了jol包,你需要在你的pom文件当中引入对应的包:

<dependency>
  <groupId>org.openjdk.jol</groupId>
  <artifactId>jol-core</artifactId>
  <version>0.10</version>
</dependency>

上图当中我们显示的结果是在64位机器下面显示的结果,在64位机器当中在Java对象头当中的MarkWord和Klcass Pointer内存布局如下:

What is the principle and process of Java Synchronized lock upgrade?

其中MarkWord占8个字节,Kclass Pointer占4个字节。JVM在64位和32位机器上的MarkWord内容基本一致,64位机器上和32位机器上的MarkWord内容和表示意义是一样的,因此最后三位的意义你可以参考32位JVM的MarkWord。

锁升级过程

偏向锁

假如你写的synchronized代码块没有多个线程执行,而只有一个线程执行的时候这种锁对程序性能的提高还是非常大的。他的具体做法是JVM会将对象头当中的第三个用于表示是否为偏向锁的比特位设置为1,同时会使用CAS操作将线程的ID记录到Mark Word当中,如果操作成功就相当于获得????了,那么下次这个线程想进入临界区就只需要比较一下线程ID是否相同了,而不需要进行CAS或者加锁这样花费比较大的操作了,只需要进行一个简单的比较即可,这种情况下加锁的开销非常小。

What is the principle and process of Java Synchronized lock upgrade?

可能你会有一个疑问在无锁的状态下Mark Word存储的是哈希值,而在偏向锁的状态下存储的是线程的ID,那么之前存储的Hash Code不就没有了嘛!你可能会想没有就没有吧,再算一遍不就行了!事实上不是这样,如果我们计算过哈希值之后我们需要尽量保持哈希值不变(但是这个在Java当中并没有强制,因为在Java当中可以重写hashCode方法),因此在Java当中为了能够保持哈希值的不变性就会在第一次计算一致性哈希值(Mark Word里面存储的是一致性哈希值,并不是指重写的hashCode返回值,在Java当中可以通过 Object.hashCode()或者System.identityHashCode(Object)方法计算一致性哈希值)的时候就将计算出来的一致性哈希值存储到Mark Word当中,下一次再有一致性哈希值的请求的时候就将存储下来的一致性哈希值返回,这样就可以保证每次计算的一致性哈希值相同。但是在变成偏向锁的时候会使用线程ID覆盖哈希值,因此当一个对象计算过一致性哈希值之后,他就再也不能进行偏向锁状态,而且当一个对象正处于偏向锁状态的时候,收到了一致性哈希值的请求的时候,也就是调用上面提到的两个方法,偏向锁就会立马膨胀为重量级锁,然后将Mark Word 储在重量级锁里。

下面的代码就是验证当在偏向锁的状态调用System.identityHashCode函数锁的状态就会升级为重量级锁:

import org.openjdk.jol.info.ClassLayout;
 
import java.util.concurrent.TimeUnit;
 
public class MarkWord {
 
  public Object o = new Object();
 
  public synchronized void demo() {
 
    System.out.println("System.identityHashCode(o) 函数之前");
    System.out.println(ClassLayout.parseInstance(o).toPrintable());
    synchronized (o) {
      System.identityHashCode(o);
      System.out.println("System.identityHashCode(o) 函数之后");
      System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
  }
 
  public static void main(String[] args) throws InterruptedException {
    TimeUnit.SECONDS.sleep(5);
 
    MarkWord markWord = new MarkWord();
    Thread thread = new Thread(markWord::demo);
    thread.start();
    thread.join();
    TimeUnit.SECONDS.sleep(2);
    System.out.println(ClassLayout.parseInstance(markWord.o).toPrintable());
  }
}

What is the principle and process of Java Synchronized lock upgrade?

轻量级锁

轻量级锁也是在JDK1.6加入的,当一个线程获取偏向锁的时候,有另外的线程加入锁的竞争时,这个时候就会从偏向锁升级为轻量级锁。

What is the principle and process of Java Synchronized lock upgrade?

在轻量级锁的状态时,虚拟机首先会在当前线程的栈帧当中建立一个锁记录(Lock Record),用于存储对象MarkWord的拷贝,官方称这个为Displaced Mark Word。然后虚拟机会使用CAS操作尝试将对象的MarkWord指向栈中的Lock Record,如果操作成功说明这个线程获取到了锁,能够进入同步代码块执行,否则说明这个锁对象已经被其他线程占用了,线程就需要使用CAS不断的进行获取锁的操作,当然你可能会有疑问,难道就让线程一直死循环了吗?这对CPU的花费那不是太大了吗,确实是这样的因此在CAS满足一定条件的时候轻量级锁就会升级为重量级锁,具体过程在重量级锁章节中分析。

当线程需要从同步代码块出来的时候,线程同样的需要使用CAS将Displaced Mark Word替换回对象的MarkWord,如果替换成功,那么同步过程就完成了,如果替换失败就说明有其他线程尝试获取该锁,而且锁已经升级为重量级锁,此前竞争锁的线程已经被挂起,因此线程在释放锁的同时还需要将挂起的线程唤醒。

重量级锁

所谓重量级锁就是一种开销最大的锁机制,在这种情况下需要操作系统将没有进入同步代码块的线程挂起,JVM(Linux操作系统下)底层是使用pthread_mutex_lockpthread_mutex_unlockpthread_cond_waitpthread_cond_signalpthread_cond_broadcast这几个库函数实现的,而这些函数依赖于futex系统调用,因此在使用重量级锁的时候因为进行了系统调用,进程需要从用户态转为内核态将线程挂起,然后从内核态转为用户态,当解锁的时候又需要从用户态转为内核态将线程唤醒,这一来二去的花费就比较大了(和CAS自旋锁相比)。

What is the principle and process of Java Synchronized lock upgrade?

在有两个以上的线程竞争同一个轻量级锁的情况下,轻量级锁不再有效(轻量级锁升级的一个条件),这个时候锁为膨胀成重量级锁,锁的标志状态变成10,MarkWord当中存储的就是指向重量级锁的指针,后面等待锁的线程就会被挂起。

因为这个时候MarkWord当中存储的已经是指向重量级锁的指针,因此在轻量级锁的情况下进入到同步代码块在出同步代码块的时候使用CAS将Displaced Mark Word替换回对象的MarkWord的时候就会替换失败,在前文已经提到,在失败的情况下,线程在释放锁的同时还需要将被挂起的线程唤醒。

The above is the detailed content of What is the principle and process of Java Synchronized lock upgrade?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete