亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java類鎖、對(duì)象鎖、私有鎖沖突測試

 更新時(shí)間:2014年10月20日 10:47:53   投稿:junjie  
這篇文章主要介紹了Java類鎖、對(duì)象鎖、私有鎖沖突測試,得出結(jié)論是加鎖方法夠成了競爭關(guān)系,同一時(shí)刻只能有一個(gè)方法能執(zhí)行,需要的朋友可以參考下

類鎖和對(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

復(fù)制代碼 代碼如下:

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)行不同的測試)

復(fù)制代碼 代碼如下:

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,包括各種加鎖方法

復(fù)制代碼 代碼如下:

public class LockTestClass {
 //用于類鎖計(jì)數(shù)
 private static int i = 0;
    //私有鎖
 private Object object = new Object();

 /**
  * &lt;p&gt;
  * 無鎖方法
  *
  * @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方法修改如下:

復(fù)制代碼 代碼如下:

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();
 }

終端輸出:

復(fù)制代碼 代碼如下:

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方法修改如下:

復(fù)制代碼 代碼如下:

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();
 }

終端輸出:

復(fù)制代碼 代碼如下:

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方法修改如下:

復(fù)制代碼 代碼如下:

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();
 }

終端輸出:

復(fù)制代碼 代碼如下:

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)文章

最新評(píng)論