美文网首页
Synchronized

Synchronized

作者: 0青衣小褂0 | 来源:发表于2022-04-21 11:34 被阅读0次

对象的内存布局

对象的内存布局

Mark Word

32位JVM的Mark Word 64位JVM的Mark Word

Synchronized锁膨胀过程

锁膨胀过程 轻量级锁膨胀流程 重量级锁膨胀流程 锁膨胀过程

Synchronized加锁的几种方式

// 锁对象为对象A
public class A {
    private int i;
    public synchronized void increase(){
        i++;
    }
}

// 锁对象为对象A
public class A {
    private int i;
    public void increase(){
        synchronized (this) {
            i++;
        }
    }
}

// 锁对象为对象obj 
public class A {
    private int i;
    private Object obj = new Object();
    public void increase(){
        synchronized (obj) {
            i++;
        }
    }
}

//  锁对象为A.class
public class A {
    private int i;
    public synchronized static void increase(){
        i++;
    }
}

//  锁对象为A.class
public class A {
    private int i;
    public void increase(){
        synchronized (A.class) {
            i++;
        }
    }
}
QQ截图20210227014451.png

相关文章

网友评论

      本文标题:Synchronized

      本文链接:https://www.haomeiwen.com/subject/vibpfltx.html