synchronized的對(duì)象鎖和類鎖的區(qū)別

synchronized可以鎖對(duì)象,代碼塊,類對(duì)象 ,那么他們用起來會(huì)有區(qū)別嗎,這里我們用實(shí)際代碼來探究下

package com.example.hxk.thread.synchroized;

public class SyncTest1 {

    // synchronized修飾非靜態(tài)方法
    public synchronized void test1() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    // synchronized代碼塊
    public void test2() {
        synchronized (this) {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        SyncTest1 t1 = new SyncTest1();

        new Thread(new Runnable() {
            @Override
            public void run() {
                t1.test1();
            }
        }, "thread1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                t1.test2();
            }
        }, "thread2").start();
    }
}

運(yùn)行結(jié)果:

thread1 : 0
thread1 : 1
thread1 : 2
thread1 : 3
thread1 : 4
thread2 : 0
thread2 : 1
thread2 : 2
thread2 : 3
thread2 : 4

這里thread2會(huì)等thread1運(yùn)行完成才會(huì)開始運(yùn)行,說明thread1和thread2請(qǐng)求的是同一把鎖,也就說明了 synchronized代碼塊鎖當(dāng)前對(duì)象和鎖非靜態(tài)方法,他們的效果是一樣的, 鎖的都是當(dāng)前對(duì)象。

然后我們?cè)賮砜纯搭愭i,修改代碼如下

package com.example.hxk.thread.synchroized;

public class SyncTest1 {

    // 修飾靜態(tài)方法
    public static synchronized void test1() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    // 修飾類對(duì)象
    public void test2() {
        synchronized (SyncTest1.class) {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        SyncTest1 t1 = new SyncTest1();

        new Thread(new Runnable() {
            @Override
            public void run() {
                SyncTest1.test1();
            }
        }, "thread1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                t1.test2();
            }
        }, "thread2").start();
    }
}

運(yùn)行結(jié)果:

thread1 : 0
thread1 : 1
thread1 : 2
thread1 : 3
thread1 : 4
thread2 : 0
thread2 : 1
thread2 : 2
thread2 : 3
thread2 : 4

這里可以看到thread2也是被thread1阻塞,所以他們持有的是同一把鎖,也就說明synchronized修飾靜態(tài)方法和鎖類對(duì)象,他們的效果是一樣的。 接下來我們同時(shí)用類鎖和對(duì)象鎖試試,代碼如下

package com.example.hxk.thread.synchroized;

public class SyncTest1 {

    // 修飾靜態(tài)方法
    public static synchronized void test1() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    // 修飾類對(duì)象
    public synchronized void test2() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        SyncTest1 t1 = new SyncTest1();

        new Thread(new Runnable() {
            @Override
            public void run() {
                SyncTest1.test1();
            }
        }, "thread1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                t1.test2();
            }
        }, "thread2").start();
    }
}

運(yùn)行結(jié)果:

thread1 : 0
thread2 : 0
thread1 : 1
thread2 : 1
thread2 : 2
thread1 : 2
thread2 : 3
thread1 : 3
thread2 : 4
thread1 : 4

運(yùn)行結(jié)果是交替進(jìn)行的,說明對(duì)象鎖和類鎖鎖的不是同一個(gè)鎖,他們是兩個(gè)鎖,互不影響

總結(jié):
1,synchronized修飾在非靜態(tài)方法上和synchronized(this){} 同步代碼塊效果是一樣的

2,synchronized修飾在靜態(tài)方法上和 synchronized (SyncTest1.class) {} 同步代碼塊效果是一樣的

3,synchronized修飾在非靜態(tài)方法表示鎖的是當(dāng)前對(duì)象,修飾靜態(tài)方法表示鎖的是類對(duì)象(一個(gè)類在jvm中只有一個(gè)class對(duì)象)

來源:https://www.tuicool.com/articles/e6j2YnR

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容