Deep analysis of Java synchronized implementation principle

Implementation principle of Java synchronized

public class SynchronizedTest {
    public synchronized void test1(){

    }

    public void test2(){
        synchronized (this){

        }
    }
}

1) Synchronous code block

2) Synchronization method

Java object header

Mark Word

Monitor

Lock optimization

Spin lock

Adaptive spin lock

Lock elimination

public void vectortest(){
    Vector<String> vector = new Vector<String>();
    for(int i = 0 ; i < 10 ; i++){
        vector.add(i + "");
    }

    System.out.println(vector);
}

Lock coarsening

Lightweight Locking

Bias lock

Heavyweight lock

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>