Java類鎖、對(duì)象鎖、私有鎖沖突測(cè)試
類鎖和對(duì)象鎖是否會(huì)沖突?對(duì)象鎖和私有鎖是否會(huì)沖突?通過(guò)實(shí)例來(lái)進(jìn)行說(shuō)明。
一、相關(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()。
二、測(cè)試代碼
1.編寫(xiě)一個(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.編寫(xiě)一個(gè)線程類ObjThread,用于啟動(dòng)同步方法(注意它的run方法可能會(huì)調(diào)整以進(jìn)行不同的測(cè)試)
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() {
//無(wú)鎖方法
// 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.再編寫(xiě)一個(gè)鎖的測(cè)試類LockTestClass,包括各種加鎖方法
public class LockTestClass {
//用于類鎖計(jì)數(shù)
private static int i = 0;
//私有鎖
private Object object = new Object();
/**
* <p>
* 無(wú)鎖方法
*
* @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)來(lái)加鎖
*/
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.");
}
}
三、測(cè)試結(jié)果
1.測(cè)試類鎖和對(duì)象鎖,ObjectThread的run方法修改如下:
public void run() {
//無(wú)鎖方法
// 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),說(shuō)明二者使用的并非同一個(gè)鎖,不會(huì)產(chǎn)生競(jìng)爭(zhēng)。
結(jié)論:類鎖和對(duì)象鎖不會(huì)產(chǎn)生競(jìng)爭(zhēng),二者的加鎖方法不會(huì)相互影響。
2.私有鎖和對(duì)象鎖,ObjectThread的run方法修改如下:
public void run() {
//無(wú)鎖方法
// 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)生競(jìng)爭(zhēng),二者的加鎖方法不會(huì)相互影響。
3.synchronized直接加在方法上和synchronized(this),ObjectThread的run方法修改如下:
public void run() {
//無(wú)鎖方法
// 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并不是確定的,取決于誰(shuí)獲得了鎖)。
結(jié)論:synchronized直接加在方法上和synchronized(this)都是對(duì)當(dāng)前對(duì)象加鎖,二者的加鎖方法夠成了競(jìng)爭(zhēng)關(guān)系,同一時(shí)刻只能有一個(gè)方法能執(zhí)行。
相關(guān)文章
Java 輸入多行字符串或者多個(gè)int數(shù)值的方法
今天小編就為大家分享一篇Java 輸入多行字符串或者多個(gè)int數(shù)值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Java執(zhí)行cmd命令的舉例與注意事項(xiàng)
Java應(yīng)用程序主要是通過(guò)Runtime和Process兩個(gè)類來(lái)執(zhí)行cmd命令,下面這篇文章主要給大家介紹了關(guān)于Java執(zhí)行cmd命令的方法與注意事項(xiàng),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08

