synchronized原理与JAVA中的锁升级过程

JAVA对象模型:

 

 

 

其中Mark Word:

 

 

 

 

 

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.openjdk.jol.info.ClassLayout;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

@Slf4j
public class InitTest {




    /**
     * 无锁态
     */
    @Test
    public void test(){
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object o = new Object();
        log.info(String.valueOf(o.hashCode()));
        log.info(ClassLayout.parseInstance(o).toPrintable());
    }

    /**
     ** 偏向锁会延迟大约4秒启动
     ** 匿名偏向锁
     *  偏向锁
     */
    @Test
    public void test1(){
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object o = new Object();
        log.info(ClassLayout.parseInstance(o).toPrintable());
        synchronized (o){
            log.info(ClassLayout.parseInstance(o).toPrintable());
        }
    }

    /**
     * 锁竞争相对不激烈。确保一个线程执行完,另一个线程才开始执行
     * 匿名偏向锁
     * 00000101 00000000 00000000 00000000
     *
     * 偏向锁
     * 00000101 10011000 11000001 00001010
     *
     * 偏向锁未改变
     * 00000101 10011000 11000001 00001010
     *
     * 轻量级锁
     * 10010000 11101110 01011100 00001100
     */
    @Test
    public void test2(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object o = new Object();
        log.info(ClassLayout.parseInstance(o).toPrintable());

        new Thread(()->{
            synchronized (o){
                log.info(ClassLayout.parseInstance(o).toPrintable());
            }
        }).start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info(ClassLayout.parseInstance(o).toPrintable());


        new Thread(()->{
            synchronized (o){
                log.info(ClassLayout.parseInstance(o).toPrintable());
            }
        }).start();
    }
    /**
     * 无锁状态升级成重量级锁
     * 00011010 11110101 00100110 00000111
     * 00011010 11110101 00100110 00000111
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException{
        Thread.sleep(5000);
        Object a = new Object();

        Thread thread1 = new Thread(){
            @Override
            public void run() {
                synchronized (a){
                    System.out.println("thread1 locking");
                    System.out.println(ClassLayout.parseInstance(a).toPrintable());
                    try {
                        //让线程晚点儿死亡,造成锁的竞争
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread2 = new Thread(){
            @Override
            public void run() {
                synchronized (a){
                    System.out.println("thread2 locking");
                    System.out.println(ClassLayout.parseInstance(a).toPrintable());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread1.start();
        thread2.start();
    }

}
锁升级(锁膨胀)示例代码

 

posted @ 2021-03-02 14:57  Java民工陆小凤  阅读(265)  评论(0编辑  收藏  举报