Java 之 synchronized 詳解

一、概念

synchronized 是 Java 中的關(guān)鍵字,是利用鎖的機(jī)制來實現(xiàn)同步的。

鎖機(jī)制有如下兩種特性:

  • 互斥性:即在同一時間只允許一個線程持有某個對象鎖,通過這種特性來實現(xiàn)多線程中的協(xié)調(diào)機(jī)制,這樣在同一時間只有一個線程對需同步的代碼塊(復(fù)合操作)進(jìn)行訪問?;コ庑晕覀円餐Q為操作的原子性。

  • 可見性:必須確保在鎖被釋放之前,對共享變量所做的修改,對于隨后獲得該鎖的另一個線程是可見的(即在獲得鎖時應(yīng)獲得最新共享變量的值),否則另一個線程可能是在本地緩存的某個副本上繼續(xù)操作從而引起不一致。

二、對象鎖和類鎖

1. 對象鎖

在 Java 中,每個對象都會有一個 monitor 對象,這個對象其實就是 Java 對象的鎖,通常會被稱為“內(nèi)置鎖”或“對象鎖”。類的對象可以有多個,所以每個對象有其獨立的對象鎖,互不干擾。

2. 類鎖

在 Java 中,針對每個類也有一個鎖,可以稱為“類鎖”,類鎖實際上是通過對象鎖實現(xiàn)的,即類的 Class 對象鎖。每個類只有一個 Class 對象,所以每個類只有一個類鎖。

三、synchronized 的用法分類

synchronized 的用法可以從兩個維度上面分類:

1. 根據(jù)修飾對象分類

synchronized 可以修飾方法和代碼塊

  • 修飾代碼塊

    • synchronized(this|object) {}

    • synchronized(類.class) {}

  • 修飾方法

    • 修飾非靜態(tài)方法

    • 修飾靜態(tài)方法

2. 根據(jù)獲取的鎖分類

  • 獲取對象鎖

    • synchronized(this|object) {}

    • 修飾非靜態(tài)方法

  • 獲取類鎖

    • synchronized(類.class) {}

    • 修飾靜態(tài)方法

四、synchronized 的用法詳解

這里根據(jù)獲取的鎖分類來分析 synchronized 的用法

1. 獲取對象鎖

1.1 對于同一對象

  • 同步線程類:
class SyncThread implements Runnable {

    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();
        if (threadName.startsWith("A")) {
            async();
        } else if (threadName.startsWith("B")) {
            sync1();
        } else if (threadName.startsWith("C")) {
            sync2();
        }

    }

    /**
     * 異步方法
     */
    private void async() {
        try {
            System.out.println(Thread.currentThread().getName() + "_Async_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "_Async_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 方法中有 synchronized(this|object) {} 同步代碼塊
     */
    private void sync1() {
        System.out.println(Thread.currentThread().getName() + "_Sync1: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        synchronized (this) {
            try {
                System.out.println(Thread.currentThread().getName() + "_Sync1_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName() + "_Sync1_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * synchronized 修飾非靜態(tài)方法
     */
    private synchronized void sync2() {
        System.out.println(Thread.currentThread().getName() + "_Sync2: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        try {
            System.out.println(Thread.currentThread().getName() + "_Sync2_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "_Sync2_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
  • 測試代碼:
public class SyncTest {

    public static void main(String... args) {
        SyncThread syncThread = new SyncThread();
        Thread A_thread1 = new Thread(syncThread, "A_thread1");
        Thread A_thread2 = new Thread(syncThread, "A_thread2");
        Thread B_thread1 = new Thread(syncThread, "B_thread1");
        Thread B_thread2 = new Thread(syncThread, "B_thread2");
        Thread C_thread1 = new Thread(syncThread, "C_thread1");
        Thread C_thread2 = new Thread(syncThread, "C_thread2");
        A_thread1.start();
        A_thread2.start();
        B_thread1.start();
        B_thread2.start();
        C_thread1.start();
        C_thread2.start();
    }
}
  • 運行結(jié)果:
B_thread2_Sync1: 14:44:20
A_thread1_Async_Start: 14:44:20
B_thread1_Sync1: 14:44:20
C_thread1_Sync2: 14:44:20
A_thread2_Async_Start: 14:44:20
C_thread1_Sync2_Start: 14:44:20
A_thread1_Async_End: 14:44:22
A_thread2_Async_End: 14:44:22
C_thread1_Sync2_End: 14:44:22
B_thread1_Sync1_Start: 14:44:22
B_thread1_Sync1_End: 14:44:24
B_thread2_Sync1_Start: 14:44:24
B_thread2_Sync1_End: 14:44:26
C_thread2_Sync2: 14:44:26
C_thread2_Sync2_Start: 14:44:26
C_thread2_Sync2_End: 14:44:28
  • 結(jié)果分析:

    • A 類線程訪問方法中沒有同步代碼塊,A 類線程是異步的,所以有線程訪問對象的同步代碼塊時,另外的線程可以訪問該對象的非同步代碼塊:
    A_thread1_Async_Start: 14:44:20
    A_thread2_Async_Start: 14:44:20
    A_thread1_Async_End: 14:44:22
    A_thread2_Async_End: 14:44:22
    
    • B 類線程訪問的方法中有同步代碼塊,B 類線程是同步的,一個線程在訪問對象的同步代碼塊,另一個訪問對象的同步代碼塊的線程會被阻塞:
    B_thread1_Sync1_Start: 14:44:22
    B_thread1_Sync1_End: 14:44:24
    B_thread2_Sync1_Start: 14:44:24
    B_thread2_Sync1_End: 14:44:26
    
    • synchronized(this|object) {} 代碼塊 {} 之外的代碼依然是異步的:
    B_thread2_Sync1: 14:44:20
    B_thread1_Sync1: 14:44:20
    
    • C 類線程訪問的是 synchronized 修飾非靜態(tài)方法,C 類線程是同步的,一個線程在訪問對象的同步代方法,另一個訪問對象同步方法的線程會被阻塞:
    C_thread1_Sync2_Start: 14:44:20
    C_thread1_Sync2_End: 14:44:22
    C_thread2_Sync2_Start: 14:44:26
    C_thread2_Sync2_End: 14:44:28
    
    • synchronized 修飾非靜態(tài)方法,作用范圍是整個方法,所以方法中所有的代碼都是同步的:
    C_thread1_Sync2: 14:44:20
    C_thread2_Sync2: 14:44:26
    
    • 由結(jié)果可知 B 類和 C 類線程順序執(zhí)行,<strong>類中 synchronized(this|object) {} 代碼塊和 synchronized 修飾非靜態(tài)方法獲取的鎖是同一個鎖,即該類的對象的對象鎖。</strong>所以 B 類線程和 C 類線程也是同步的:
    B_thread1_Sync1_Start: 14:44:22
    B_thread1_Sync1_End: 14:44:24
    C_thread1_Sync2_Start: 14:44:20
    C_thread1_Sync2_End: 14:44:22
    B_thread2_Sync1_Start: 14:44:24
    B_thread2_Sync1_End: 14:44:26
    C_thread2_Sync2_Start: 14:44:26
    C_thread2_Sync2_End: 14:44:28
    

1.2 對于不同對象

  • 修改測試代碼為:
public class SyncTest {

    public static void main(String... args) {
        Thread A_thread1 = new Thread(new SyncThread(), "A_thread1");
        Thread A_thread2 = new Thread(new SyncThread(), "A_thread2");
        Thread B_thread1 = new Thread(new SyncThread(), "B_thread1");
        Thread B_thread2 = new Thread(new SyncThread(), "B_thread2");
        Thread C_thread1 = new Thread(new SyncThread(), "C_thread1");
        Thread C_thread2 = new Thread(new SyncThread(), "C_thread2");
        A_thread1.start();
        A_thread2.start();
        B_thread1.start();
        B_thread2.start();
        C_thread1.start();
        C_thread2.start();
    }
}
  • 運行結(jié)果:
A_thread2_Async_Start: 15:01:34
C_thread2_Sync2: 15:01:34
B_thread2_Sync1: 15:01:34
C_thread1_Sync2: 15:01:34
B_thread2_Sync1_Start: 15:01:34
B_thread1_Sync1: 15:01:34
C_thread1_Sync2_Start: 15:01:34
A_thread1_Async_Start: 15:01:34
C_thread2_Sync2_Start: 15:01:34
B_thread1_Sync1_Start: 15:01:34
C_thread1_Sync2_End: 15:01:36
A_thread1_Async_End: 15:01:36
C_thread2_Sync2_End: 15:01:36
B_thread2_Sync1_End: 15:01:36
B_thread1_Sync1_End: 15:01:36
A_thread2_Async_End: 15:01:36
  • 結(jié)果分析:

    • 兩個線程訪問不同對象的 synchronized(this|object) {} 代碼塊和 synchronized 修飾非靜態(tài)方法是異步的,同一個類的不同對象的對象鎖互不干擾。

2 獲取類鎖

2.1 對于同一對象

  • 同步線程類:
class SyncThread implements Runnable {

    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();
        if (threadName.startsWith("A")) {
            async();
        } else if (threadName.startsWith("B")) {
            sync1();
        } else if (threadName.startsWith("C")) {
            sync2();
        }

    }

    /**
     * 異步方法
     */
    private void async() {
        try {
            System.out.println(Thread.currentThread().getName() + "_Async_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "_Async_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 方法中有 synchronized(類.class) {} 同步代碼塊
     */
    private void sync1() {
        System.out.println(Thread.currentThread().getName() + "_Sync1: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        synchronized (SyncThread.class) {
            try {
                System.out.println(Thread.currentThread().getName() + "_Sync1_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName() + "_Sync1_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * synchronized 修飾靜態(tài)方法
     */
    private synchronized static void sync2() {
        System.out.println(Thread.currentThread().getName() + "_Sync2: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        try {
            System.out.println(Thread.currentThread().getName() + "_Sync2_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "_Sync2_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
  • 測試代碼:
public class SyncTest {

    public static void main(String... args) {
        SyncThread syncThread = new SyncThread();
        Thread A_thread1 = new Thread(syncThread, "A_thread1");
        Thread A_thread2 = new Thread(syncThread, "A_thread2");
        Thread B_thread1 = new Thread(syncThread, "B_thread1");
        Thread B_thread2 = new Thread(syncThread, "B_thread2");
        Thread C_thread1 = new Thread(syncThread, "C_thread1");
        Thread C_thread2 = new Thread(syncThread, "C_thread2");
        A_thread1.start();
        A_thread2.start();
        B_thread1.start();
        B_thread2.start();
        C_thread1.start();
        C_thread2.start();
    }
}
  • 運行結(jié)果:
B_thread1_Sync1: 15:08:13
C_thread1_Sync2: 15:08:13
B_thread2_Sync1: 15:08:13
A_thread1_Async_Start: 15:08:13
C_thread1_Sync2_Start: 15:08:13
A_thread2_Async_Start: 15:08:13
C_thread1_Sync2_End: 15:08:15
A_thread2_Async_End: 15:08:15
A_thread1_Async_End: 15:08:15
B_thread2_Sync1_Start: 15:08:15
B_thread2_Sync1_End: 15:08:17
B_thread1_Sync1_Start: 15:08:17
B_thread1_Sync1_End: 15:08:19
C_thread2_Sync2: 15:08:19
C_thread2_Sync2_Start: 15:08:19
C_thread2_Sync2_End: 15:08:21
  • 結(jié)果分析:

    • 由結(jié)果可以看出,在同一對象的情況下,synchronized(類.class) {} 代碼塊或 synchronized 修飾靜態(tài)方法和 synchronized(this|object) {} 代碼塊和 synchronized 修飾非靜態(tài)方法的行為一致。

2.2 對于不同對象

  • 修改測試代碼為:
public class SyncTest {

    public static void main(String... args) {
        Thread A_thread1 = new Thread(new SyncThread(), "A_thread1");
        Thread A_thread2 = new Thread(new SyncThread(), "A_thread2");
        Thread B_thread1 = new Thread(new SyncThread(), "B_thread1");
        Thread B_thread2 = new Thread(new SyncThread(), "B_thread2");
        Thread C_thread1 = new Thread(new SyncThread(), "C_thread1");
        Thread C_thread2 = new Thread(new SyncThread(), "C_thread2");
        A_thread1.start();
        A_thread2.start();
        B_thread1.start();
        B_thread2.start();
        C_thread1.start();
        C_thread2.start();
    }
}
  • 運行結(jié)果:
A_thread2_Async_Start: 15:17:28
B_thread2_Sync1: 15:17:28
A_thread1_Async_Start: 15:17:28
B_thread1_Sync1: 15:17:28
C_thread1_Sync2: 15:17:28
C_thread1_Sync2_Start: 15:17:28
C_thread1_Sync2_End: 15:17:30
A_thread2_Async_End: 15:17:30
B_thread1_Sync1_Start: 15:17:30
A_thread1_Async_End: 15:17:30
B_thread1_Sync1_End: 15:17:32
B_thread2_Sync1_Start: 15:17:32
B_thread2_Sync1_End: 15:17:34
C_thread2_Sync2: 15:17:34
C_thread2_Sync2_Start: 15:17:34
C_thread2_Sync2_End: 15:17:36
  • 結(jié)果分析:

    • <strong>兩個線程訪問不同對象的 synchronized(類.class) {} 代碼塊或 synchronized 修飾靜態(tài)方法還是同步的,類中 synchronized(類.class) {} 代碼塊和 synchronized 修飾靜態(tài)方法獲取的鎖是類鎖。對于同一個類的不同對象的類鎖是同一個。</strong>

3 類中同時有 synchronized(類.class) {} 代碼塊或 synchronized 修飾靜態(tài)方法和 synchronized(this|object) {} 代碼塊和 synchronized 修飾非靜態(tài)方法時會怎樣?

  • 修改同步線程類:
class SyncThread implements Runnable {

    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();
        if (threadName.startsWith("A")) {
            async();
        } else if (threadName.startsWith("B")) {
            sync1();
        } else if (threadName.startsWith("C")) {
            sync2();
        }

    }

    /**
     * 異步方法
     */
    private void async() {
        try {
            System.out.println(Thread.currentThread().getName() + "_Async_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "_Async_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * synchronized 修飾非靜態(tài)方法
     */
    private synchronized void sync1() {
        try {
            System.out.println(Thread.currentThread().getName() + "_Sync1_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "_Sync1_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * synchronized 修飾靜態(tài)方法
     */
    private synchronized static void sync2() {
        try {
            System.out.println(Thread.currentThread().getName() + "_Sync2_Start: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "_Sync2_End: " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
  • 修改測試代碼:
public class SyncTest {

    public static void main(String... args) {
        Thread B_thread1 = new Thread(syncThread, "B_thread1");
        Thread C_thread1 = new Thread(syncThread, "C_thread1");
        B_thread1.start();
        C_thread1.start();
    }
}
  • 運行結(jié)果:
B_thread1_Sync1_Start: 15:35:21
C_thread1_Sync2_Start: 15:35:21
B_thread1_Sync1_End: 15:35:23
C_thread1_Sync2_End: 15:35:23
  • 運行結(jié)果分析:

    • 由結(jié)果可以看到 B 類線程和 C 類線程是異步的,即 synchronized 修飾靜態(tài)方法和 synchronized 修飾非靜態(tài)方法是異步的,對于 synchronized(類.class) {} 代碼塊和 synchronized(this|object) {} 代碼塊也是一樣的。<strong>所以對象鎖和類鎖是獨立的,互不干擾。</strong>

4 補(bǔ)充

  1. synchronized關(guān)鍵字不能繼承。

    對于父類中的 synchronized 修飾方法,子類在覆蓋該方法時,默認(rèn)情況下不是同步的,必須顯示的使用 synchronized 關(guān)鍵字修飾才行。

  2. 在定義接口方法時不能使用synchronized關(guān)鍵字。

  3. 構(gòu)造方法不能使用synchronized關(guān)鍵字,但可以使用synchronized代碼塊來進(jìn)行同步。

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

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

  • Java8張圖 11、字符串不變性 12、equals()方法、hashCode()方法的區(qū)別 13、...
    Miley_MOJIE閱讀 3,910評論 0 11
  • 本文主要講了java中多線程的使用方法、線程同步、線程數(shù)據(jù)傳遞、線程狀態(tài)及相應(yīng)的一些線程函數(shù)用法、概述等。 首先講...
    李欣陽閱讀 2,602評論 1 15
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,822評論 18 399
  • Java多線程學(xué)習(xí) [-] 一擴(kuò)展javalangThread類 二實現(xiàn)javalangRunnable接口 三T...
    影馳閱讀 3,115評論 1 18
  • (一)Java部分 1、列舉出JAVA中6個比較常用的包【天威誠信面試題】 【參考答案】 java.lang;ja...
    獨云閱讀 7,275評論 0 62

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