Java類鎖、對(duì)象鎖、私有鎖沖突測試
類鎖和對(duì)象鎖是否會(huì)沖突?對(duì)象鎖和私有鎖是否會(huì)沖突?通過實(shí)例來進(jìn)行說明。
一、相關(guān)約定
為了明確后文的描述,先對(duì)本文涉及到的鎖的相關(guān)定義作如下約定:
1. 類鎖:在代碼中的方法上加了static和synchronized的鎖,或者synchronized(xxx.class)的代碼段,如下文中的increament();
2.對(duì)象鎖:在代碼中的方法上加了synchronized的鎖,或者synchronized(this)的代碼段,如下文中的synOnMethod()和synInMethod();
3.私有鎖:在類內(nèi)部聲明一個(gè)私有屬性如private Object lock,在需要加鎖的代碼段synchronized(lock),如下文中的synMethodWithObj()。
二、測試代碼
1.編寫一個(gè)啟動(dòng)類ObjectLock
public class ObjectLock {
public static void main(String[] args) {
System.out.println("start time = " + System.currentTimeMillis()+"ms");
LockTestClass test = new LockTestClass();
for (int i = 0; i < 3; i++) {
Thread thread = new ObjThread(test, i);
thread.start();
}
}
}
2.編寫一個(gè)線程類ObjThread,用于啟動(dòng)同步方法(注意它的run方法可能會(huì)調(diào)整以進(jìn)行不同的測試)
public class ObjThread extends Thread {
LockTestClass lock;
int i = 0;
public ObjThread(LockTestClass lock, int i) {
this.lock = lock;
this.i = i;
}
public void run() {
//無鎖方法
// lock.noSynMethod(this.getId(),this);
//對(duì)象鎖方法1,采用synchronized synInMethod的方式
lock.synInMethod();
//對(duì)象鎖方法2,采用synchronized(this)的方式
// lock.synOnMethod();
//私有鎖方法,采用synchronized(object)的方式
// lock.synMethodWithObj();
//類鎖方法,采用static synchronized increment的方式
LockTestClass.increment();
}
}
3.再編寫一個(gè)鎖的測試類LockTestClass,包括各種加鎖方法
public class LockTestClass {
//用于類鎖計(jì)數(shù)
private static int i = 0;
//私有鎖
private Object object = new Object();
/**
* <p>
* 無鎖方法
*
* @param threadID
* @param thread
*/
public void noSynMethod(long threadID, ObjThread thread) {
System.out.println("nosyn: class obj is " + thread + ", threadId is"
+ threadID);
}
/**
* 對(duì)象鎖方法1
*/
public synchronized void synOnMethod() {
System.out.println("synOnMethod begins" + ", time = "
+ System.currentTimeMillis() + "ms");
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("synOnMethod ends");
}
/**
* 對(duì)象鎖方法2,采用synchronized (this)來加鎖
*/
public void synInMethod() {
synchronized (this) {
System.out.println("synInMethod begins" + ", time = "
+ System.currentTimeMillis() + "ms");
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("synInMethod ends");
}
}
/**
* 對(duì)象鎖方法3
*/
public void synMethodWithObj() {
synchronized (object) {
System.out.println("synMethodWithObj begins" + ", time = "
+ System.currentTimeMillis() + "ms");
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("synMethodWithObj ends");
}
}
/**
* 類鎖
*/
public static synchronized void increament() {
System.out.println("class synchronized. i = " + i + ", time = "
+ System.currentTimeMillis() + "ms");
i++;
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("class synchronized ends.");
}
}
三、測試結(jié)果
1.測試類鎖和對(duì)象鎖,ObjectThread的run方法修改如下:
public void run() {
//無鎖方法
// lock.noSynMethod(this.getId(),this);
//對(duì)象鎖方法1,采用synchronized synInMethod的方式
lock.synInMethod();
//對(duì)象鎖方法2,采用synchronized(this)的方式
// lock.synOnMethod();
//私有鎖方法,采用synchronized(object)的方式
// lock.synMethodWithObj();
//類鎖方法,采用static synchronized increment的方式
LockTestClass.increament();
}
終端輸出:
start time = 1413101360231ms
synInMethod begins, time = 1413101360233ms
synInMethod ends
class synchronized. i = 0, time = 1413101362233ms
synInMethod begins, time = 1413101362233ms
class synchronized ends.
synInMethod ends
class synchronized. i = 1, time = 1413101364233ms
synInMethod begins, time = 1413101364233ms
class synchronized ends.
synInMethod ends
class synchronized. i = 2, time = 1413101366234ms
class synchronized ends.
可以看到對(duì)象鎖方法(synInMothod)第一次啟動(dòng)時(shí)比類鎖方法(increament)快2秒,這是因?yàn)樵趕ynInMehtod執(zhí)行時(shí)sleep了2秒再執(zhí)行的increament,而這兩個(gè)方法共用一個(gè)線程,所以會(huì)慢2秒,如果increament在run中放到synInMethod前面,那么第一次啟動(dòng)時(shí)就是increament快2秒。
而當(dāng)類鎖方法啟動(dòng)時(shí),另一個(gè)線程時(shí)的對(duì)象鎖方法也幾乎同時(shí)啟動(dòng),說明二者使用的并非同一個(gè)鎖,不會(huì)產(chǎn)生競爭。
結(jié)論:類鎖和對(duì)象鎖不會(huì)產(chǎn)生競爭,二者的加鎖方法不會(huì)相互影響。
2.私有鎖和對(duì)象鎖,ObjectThread的run方法修改如下:
public void run() {
//無鎖方法
// lock.noSynMethod(this.getId(),this);
//對(duì)象鎖方法1,采用synchronized synInMethod的方式
lock.synInMethod();
//對(duì)象鎖方法2,采用synchronized(this)的方式
// lock.synOnMethod();
//私有鎖方法,采用synchronized(object)的方式
lock.synMethodWithObj();
//類鎖方法,采用static synchronized increment的方式
// LockTestClass.increament();
}
終端輸出:
start time = 1413121912406ms
synInMethod begins, time = 1413121912407ms.
synInMethod ends.
synMethodWithObj begins, time = 1413121914407ms
synInMethod begins, time = 1413121914407ms.
synInMethod ends.
synMethodWithObj ends
synInMethod begins, time = 1413121916407ms.
synMethodWithObj begins, time = 1413121916407ms
synInMethod ends.
synMethodWithObj ends
synMethodWithObj begins, time = 1413121918407ms
synMethodWithObj ends
和類鎖和對(duì)象鎖非常類似。
結(jié)論:私有鎖和對(duì)象鎖也不會(huì)產(chǎn)生競爭,二者的加鎖方法不會(huì)相互影響。
3.synchronized直接加在方法上和synchronized(this),ObjectThread的run方法修改如下:
public void run() {
//無鎖方法
// lock.noSynMethod(this.getId(),this);
//對(duì)象鎖方法1,采用synchronized synInMethod的方式
lock.synInMethod();
//對(duì)象鎖方法2,采用synchronized(this)的方式
lock.synOnMethod();
//私有鎖方法,采用synchronized(object)的方式
// lock.synMethodWithObj();
//類鎖方法,采用static synchronized increment的方式
// LockTestClass.increament();
}
終端輸出:
start time = 1413102913278ms
synInMethod begins, time = 1413102913279ms
synInMethod ends
synInMethod begins, time = 1413102915279ms
synInMethod ends
synOnMethod begins, time = 1413102917279ms
synOnMethod ends
synInMethod begins, time = 1413102919279ms
synInMethod ends
synOnMethod begins, time = 1413102921279ms
synOnMethod ends
synOnMethod begins, time = 1413102923279ms
synOnMethod ends
可以看到,二者嚴(yán)格地串行輸出(當(dāng)然再次執(zhí)行時(shí)先運(yùn)行synInMethod還是先運(yùn)行synOnMethod并不是確定的,取決于誰獲得了鎖)。
結(jié)論:synchronized直接加在方法上和synchronized(this)都是對(duì)當(dāng)前對(duì)象加鎖,二者的加鎖方法夠成了競爭關(guān)系,同一時(shí)刻只能有一個(gè)方法能執(zhí)行。
相關(guān)文章
Java文件讀寫IO/NIO及性能比較詳細(xì)代碼及總結(jié)
這篇文章主要介紹了Java文件讀寫IO/NIO及性能比較詳細(xì)代碼及總結(jié),具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12MyBatis異常java.sql.SQLSyntaxErrorException的問題解決
使用mybatis插入數(shù)據(jù)時(shí)出現(xiàn)java.sql.SQLSyntaxErrorException異常,本文就來介紹一下MyBatis異常的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08MybatisPlus自帶的queryWrapper實(shí)現(xiàn)時(shí)間倒序方式
這篇文章主要介紹了MybatisPlus自帶的queryWrapper實(shí)現(xiàn)時(shí)間倒序方式,具有很好的參考價(jià)值,希望對(duì)的有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01SpringBoot整合Redis實(shí)現(xiàn)熱點(diǎn)數(shù)據(jù)緩存的示例代碼
這篇文章主要介紹了SpringBoot中整合Redis實(shí)現(xiàn)熱點(diǎn)數(shù)據(jù)緩存,本文以IDEA?+?SpringBoot作為?Java中整合Redis的使用?的測試環(huán)境,結(jié)合實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03詳解Java中格式化日期的DateFormat與SimpleDateFormat類
DateFormat其本身是一個(gè)抽象類,SimpleDateFormat 類是DateFormat類的子類,一般情況下來講DateFormat類很少會(huì)直接使用,而都使用SimpleDateFormat類完成,下面我們具體來看一下兩個(gè)類的用法:2016-05-05Spring Cloud OAuth2 實(shí)現(xiàn)用戶認(rèn)證及單點(diǎn)登錄的示例代碼
這篇文章主要介紹了Spring Cloud OAuth2 實(shí)現(xiàn)用戶認(rèn)證及單點(diǎn)登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Java swing實(shí)現(xiàn)音樂播放器桌面歌詞字體變色效果
這篇文章主要為大家詳細(xì)介紹了Java swing實(shí)現(xiàn)音樂播放器桌面歌詞字體變色效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06詳談Java泛型中T和問號(hào)(通配符)的區(qū)別
下面小編就為大家?guī)硪黄斦凧ava泛型中T和問號(hào)(通配符)的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10