zoukankan      html  css  js  c++  java
  • 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();
        }
    
    }
    锁升级(锁膨胀)示例代码
  • 相关阅读:
    react-绑定this并传参的三种方式
    CSS中使用文本阴影与元素阴影
    react-native构建基本页面2---轮播图+九宫格
    HTML 事件属性
    博客园文章版权声明(js自动生成)
    js 字符串方法 和 数组方法总览
    软工期末各类图复习笔记
    AES算法描述
    DQL查询语言-基础查询、条件查询、排序查询、多表连接查询、子查询、分页查询和联合查询
    Python-模块、包和库
  • 原文地址:https://www.cnblogs.com/yibao/p/14468966.html
Copyright © 2011-2022 走看看