自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Synchronized 加鎖 This 和Class 的區(qū)別!

開發(fā) 后端
synchronized 是 Java 語言中處理并發(fā)問題的一種常用手段,它也被我們親切的稱之為“Java 內(nèi)置鎖”,由此可見其地位之高。然而 synchronized 卻有著多種用法,當(dāng)它修飾不同對(duì)象時(shí),其意義也是不同的,下面我們一起來看。

[[414654]]

本文轉(zhuǎn)載自微信公眾號(hào)「Java中文社群」,作者磊哥。轉(zhuǎn)載本文請(qǐng)聯(lián)系Java中文社群公眾號(hào)。

synchronized 是 Java 語言中處理并發(fā)問題的一種常用手段,它也被我們親切的稱之為“Java 內(nèi)置鎖”,由此可見其地位之高。然而 synchronized 卻有著多種用法,當(dāng)它修飾不同對(duì)象時(shí),其意義也是不同的,下面我們一起來看。

synchronized 用法

synchronized 可以用來修飾普通方法、靜態(tài)方法和代碼塊。

① 修飾普通方法

  1. /** 
  2.  * synchronized 修飾普通方法 
  3.  */ 
  4. public synchronized void method() { 
  5.     // ....... 

當(dāng) synchronized 修飾普通方法時(shí),被修飾的方法被稱為同步方法,其作用范圍是整個(gè)方法,作用的對(duì)象是調(diào)用這個(gè)方法的對(duì)象。

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

  1. /** 
  2.  * synchronized 修飾靜態(tài)方法 
  3.  */ 
  4. public static synchronized void staticMethod() { 
  5.     // ....... 

當(dāng) synchronized 修飾靜態(tài)的方法時(shí),其作用的范圍是整個(gè)方法,作用對(duì)象是調(diào)用這個(gè)類的所有對(duì)象。

③ 修飾代碼塊

為了減少鎖的粒度,我們可以選擇在一個(gè)方法中的某個(gè)部分使用 synchronized 來修飾(一段代碼塊),從而實(shí)現(xiàn)對(duì)一個(gè)方法中的部分代碼進(jìn)行加鎖,實(shí)現(xiàn)代碼如下:

  1. public void classMethod() throws InterruptedException { 
  2.     // 前置代碼... 
  3.      
  4.     // 加鎖代碼 
  5.     synchronized (SynchronizedExample.class) { 
  6.         // ...... 
  7.     } 
  8.      
  9.     // 后置代碼... 

以上代碼在執(zhí)行時(shí),被修飾的代碼塊稱為同步語句塊,其作用范圍是大括號(hào)“{}”括起來的代碼塊,作用的對(duì)象是調(diào)用這個(gè)代碼塊的對(duì)象。

但以上代碼,除了可以加鎖 class 之外,還可以加鎖 this,具體示例如下:

  1. public void classMethod() throws InterruptedException { 
  2.     // 前置處理代碼... 
  3.     synchronized (this) { 
  4.         // ...... 
  5.     } 
  6.     // 后置處理代碼... 

那問題來了,使用 synchronized 加鎖 this 和 class 的區(qū)別是什么?不都是加鎖同一個(gè)類嗎?

答案還真不是,加鎖 this 和 class 區(qū)別還是很大的。下面我們通過以下 4 個(gè)示例,來看二者之間的區(qū)別。

1.加鎖 class 共享一個(gè)類實(shí)例

首先,我們創(chuàng)建 5 個(gè)線程,調(diào)用同一個(gè)對(duì)象下 synchronized 加鎖的 class 代碼,具體示例如下:

  1. import java.util.Date
  2. import java.util.concurrent.TimeUnit; 
  3.  
  4. public class SynchronizedExample { 
  5.  
  6.     public static void main(String[] args) { 
  7.         // 創(chuàng)建當(dāng)前類實(shí)例 
  8.         final SynchronizedExample example = new SynchronizedExample(); 
  9.         // 創(chuàng)建 5 個(gè)線程執(zhí)行任務(wù) 
  10.         for (int i = 0; i < 5; i++) { 
  11.             new Thread(new Runnable() { 
  12.                 @Override 
  13.                 public void run() { 
  14.                     try { 
  15.                         // 調(diào)用 synchronized 修飾的 class 方法 
  16.                         example.classMethod(); 
  17.                     } catch (InterruptedException e) { 
  18.                         e.printStackTrace(); 
  19.                     } 
  20.                 } 
  21.             }).start(); 
  22.         } 
  23.     } 
  24.  
  25.     /** 
  26.      * synchronized 修飾的 class 方法 
  27.      * @throws InterruptedException 
  28.      */ 
  29.     public void classMethod() throws InterruptedException { 
  30.         synchronized (SynchronizedExample.class) { 
  31.             System.out.println(String.format("當(dāng)前執(zhí)行線程:%s,執(zhí)行時(shí)間:%s"
  32.                     Thread.currentThread().getName(), new Date())); 
  33.             TimeUnit.SECONDS.sleep(1); 
  34.         } 
  35.     } 

以上程序的執(zhí)行結(jié)果如下:

從上述結(jié)果可以看出,這 5 個(gè)線程共享的是同一把鎖。

2.加鎖 class 創(chuàng)建多個(gè)實(shí)例

接下來,我們創(chuàng)建 5 個(gè)線程,調(diào)用不同對(duì)象下 synchronized 加鎖的 class 代碼,具體示例如下:

  1. import java.util.Date
  2. import java.util.concurrent.TimeUnit; 
  3.  
  4. public class SynchronizedExample { 
  5.  
  6.     public static void main(String[] args) { 
  7.         // 創(chuàng)建 5 個(gè)線程執(zhí)行任務(wù) 
  8.         for (int i = 0; i < 5; i++) { 
  9.             new Thread(new Runnable() { 
  10.                 @Override 
  11.                 public void run() { 
  12.                     try { 
  13.                         // 創(chuàng)建類實(shí)例 
  14.                         SynchronizedExample example = new SynchronizedExample(); 
  15.                         // 調(diào)用 synchronized 修飾的 class 方法 
  16.                         example.classMethod(); 
  17.                     } catch (InterruptedException e) { 
  18.                         e.printStackTrace(); 
  19.                     } 
  20.                 } 
  21.             }).start(); 
  22.         } 
  23.     } 
  24.      
  25.     /** 
  26.      * synchronized 修飾的 class 方法 
  27.      * @throws InterruptedException 
  28.      */ 
  29.     public void classMethod() throws InterruptedException { 
  30.         synchronized (SynchronizedExample.class) { 
  31.             System.out.println(String.format("當(dāng)前執(zhí)行線程:%s,執(zhí)行時(shí)間:%s"
  32.                     Thread.currentThread().getName(), new Date())); 
  33.             TimeUnit.SECONDS.sleep(1); 
  34.         } 
  35.     } 

以上程序的執(zhí)行結(jié)果如下:

從上述結(jié)果可以看出,雖然是不同的對(duì)象,但它們使用的仍然是同一把鎖。

3.加鎖 this 共享一個(gè)類實(shí)例

接下來,我們創(chuàng)建 5 個(gè)線程,調(diào)用 synchronized 加鎖 this 的示例。首先我們這 5 個(gè)線程調(diào)用同一個(gè)對(duì)象的加鎖方法,示例代碼如下:

  1. import java.util.Date
  2. import java.util.concurrent.TimeUnit; 
  3.  
  4. public class SynchronizedExample { 
  5.  
  6.     public static void main(String[] args) { 
  7.         // 創(chuàng)建當(dāng)前類實(shí)例 
  8.         final SynchronizedExample example = new SynchronizedExample(); 
  9.         // 創(chuàng)建 5 個(gè)線程執(zhí)行任務(wù) 
  10.         for (int i = 0; i < 5; i++) { 
  11.             new Thread(new Runnable() { 
  12.                 @Override 
  13.                 public void run() { 
  14.                     try { 
  15.                         // 調(diào)用 synchronized 修飾的 this 方法 
  16.                         example.thisMethod(); 
  17.                     } catch (InterruptedException e) { 
  18.                         e.printStackTrace(); 
  19.                     } 
  20.                 } 
  21.             }).start(); 
  22.         } 
  23.     } 
  24.      
  25.     /** 
  26.      * synchronized 修飾的 this 方法 
  27.      * @throws InterruptedException 
  28.      */ 
  29.     public void thisMethod() throws InterruptedException { 
  30.         synchronized (this) { 
  31.             System.out.println(String.format("當(dāng)前執(zhí)行線程:%s,執(zhí)行時(shí)間:%s"
  32.                     Thread.currentThread().getName(), new Date())); 
  33.             TimeUnit.SECONDS.sleep(1); 
  34.         } 
  35.     } 

以上程序的執(zhí)行結(jié)果如下:

從上述結(jié)果可以看出,以上線程使用的都是同一把鎖。

4.加鎖 this 創(chuàng)建多個(gè)類實(shí)例

最后一個(gè)示例最為特殊,我們使用 synchronized 加鎖 this,讓這 5 個(gè)線程調(diào)用各自創(chuàng)建對(duì)象的方法,具體示例如下:

  1. import java.util.Date
  2. import java.util.concurrent.TimeUnit; 
  3.  
  4. public class SynchronizedExample { 
  5.  
  6.     public static void main(String[] args) { 
  7.         // 創(chuàng)建 5 個(gè)線程執(zhí)行任務(wù) 
  8.         for (int i = 0; i < 5; i++) { 
  9.             new Thread(new Runnable() { 
  10.                 @Override 
  11.                 public void run() { 
  12.                     try { 
  13.                         // 創(chuàng)建(多個(gè))類實(shí)例 
  14.                         SynchronizedExample example = new SynchronizedExample(); 
  15.                         // 調(diào)用 synchronized 修飾的 this 方法 
  16.                         example.thisMethod(); 
  17.                     } catch (InterruptedException e) { 
  18.                         e.printStackTrace(); 
  19.                     } 
  20.                 } 
  21.             }).start(); 
  22.         } 
  23.     } 
  24.      
  25.     /** 
  26.      * synchronized 修飾的 this 方法 
  27.      * @throws InterruptedException 
  28.      */ 
  29.     public void thisMethod() throws InterruptedException { 
  30.         synchronized (this) { 
  31.             System.out.println(String.format("當(dāng)前執(zhí)行線程:%s,執(zhí)行時(shí)間:%s"
  32.                     Thread.currentThread().getName(), new Date())); 
  33.             TimeUnit.SECONDS.sleep(1); 
  34.         } 
  35.     } 

以上程序的執(zhí)行結(jié)果如下:

從上述結(jié)果可以看出,當(dāng)使用 synchronized 加鎖 this 時(shí),如果線程調(diào)用的不是同一個(gè)對(duì)象,那么這些線程之間使用的鎖都是自己獨(dú)立的鎖,這個(gè)結(jié)果就和 synchronized 加鎖 class 的結(jié)果完全不同了。

總結(jié)

通過以上 4 個(gè)示例我們可以得出結(jié)論,當(dāng)使用 synchronized 加鎖 class 時(shí),無論共享一個(gè)對(duì)象還是創(chuàng)建多個(gè)對(duì)象,它們用的都是同一把鎖,而使用 synchronized 加鎖 this 時(shí),只有同一個(gè)對(duì)象會(huì)使用同一把鎖,不同對(duì)象之間的鎖是不同的。

 

責(zé)任編輯:武曉燕 來源: Java中文社群
相關(guān)推薦

2021-08-24 10:25:19

thisclassJava

2022-04-24 07:59:53

synchronizJVMAPI

2024-02-26 07:36:09

lockJava語言

2023-02-01 07:15:16

2020-03-10 10:25:38

volatileJava編程語言

2022-11-09 10:46:18

AQS加鎖機(jī)制

2021-02-01 14:10:16

JavaClass.forNaClassLoader

2021-08-31 06:37:35

Java 語言 Java 基礎(chǔ)

2009-10-10 14:40:03

C++中struct

2020-09-23 09:08:05

typescript

2010-08-30 10:52:39

CSSclassid

2010-08-23 14:16:17

DIVclassid

2021-01-14 08:58:12

Synchronize鎖操作

2010-08-23 10:57:14

CSSclassid

2009-08-27 16:22:58

interfaceabstract cl

2009-08-27 15:48:40

interfaceabstract cl

2021-03-04 08:26:17

synchronizeReentrantLojava

2017-12-06 16:28:48

Synchronize實(shí)現(xiàn)原理

2025-04-09 00:00:55

2025-01-02 12:59:55

Python面向?qū)ο缶幊?/a>type
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)