Java类锁和对象锁实践(good)

一、前言

     之前对类锁和对象锁是否是互斥的不是太确定,因此决定编写相关的程序进行实践一下。编写前对相关定义约定约定如下:

    1. 类锁:在代码中的方法上加了static和synchronized的锁,或者synchronized(xxx.class)的代码段,如下文中的increament();

    2.对象锁:在代码中的方法上加了synchronized的锁,或者synchronized(this)的代码段,如下文中的synOnMethod()和synInMethod();

    3.私有锁:在类内部声明一个私有属性如private Object lock,在需要加锁的代码段synchronized(lock),如下文中的synMethodWithObj()。

 

二、测试代码。

    1.编写一个启动类ObjectLock

Java代码  收藏代码
  1. package com.zjh.blog.practice.basic;  
  2.   
  3. public class ObjectLock {  
  4.     public static void main(String[] args) {  
  5.         System.out.println("start time = " + System.currentTimeMillis()+"ms");  
  6.         LockTestClass test = new LockTestClass();  
  7.         for (int i = 0; i < 3; i++) {  
  8.             Thread thread = new ObjThread(test, i);  
  9.             thread.start();  
  10.         }  
  11.     }  
  12. }  

 

    2.编写一个线程类ObjThread,用于启动同步方法(注意它的run方法可能会调整以进行不同的测试)

Java代码  收藏代码
  1. package com.zjh.blog.practice.basic;  
  2.   
  3. public class ObjThread extends Thread {  
  4.     LockTestClass lock;  
  5.     int i = 0;  
  6.   
  7.     public ObjThread(LockTestClass lock, int i) {  
  8.         this.lock = lock;  
  9.         this.i = i;  
  10.     }  
  11.   
  12.     public void run() {  
  13.         //无锁方法  
  14. //      lock.noSynMethod(this.getId(),this);  
  15.         //对象锁方法1,采用synchronized synInMethod的方式  
  16.         lock.synInMethod();  
  17.         //对象锁方法2,采用synchronized(this)的方式  
  18. //      lock.synOnMethod();  
  19.         //私有锁方法,采用synchronized(object)的方式  
  20. //      lock.synMethodWithObj();  
  21.         //类锁方法,采用static synchronized increment的方式  
  22.         LockTestClass.increment();  
  23.     }  
  24. }   

      

  3.再编写一个锁的测试类LockTestClass,包括各种加锁方法

 

Java代码  收藏代码
  1. package com.zjh.blog.practice.basic;  
  2.   
  3. public class LockTestClass {  
  4.     //用于类锁计数  
  5.     private static int i = 0;  
  6.     //私有锁  
  7.     private Object object = new Object();  
  8.   
  9.     /** 
  10.      * <p> 
  11.      * 无锁方法 
  12.      *  
  13.      * @param threadID 
  14.      * @param thread 
  15.      */  
  16.     public void noSynMethod(long threadID, ObjThread thread) {  
  17.         System.out.println("nosyn: class obj is " + thread + ", threadId is"  
  18.                 + threadID);  
  19.     }  
  20.   
  21.     /** 
  22.      * 对象锁方法1 
  23.      */  
  24.     public synchronized void synOnMethod() {  
  25.         System.out.println("synOnMethod begins" + ", time = "  
  26.                 + System.currentTimeMillis() + "ms");  
  27.         try {  
  28.             Thread.sleep(2000L);  
  29.         } catch (InterruptedException e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.         System.out.println("synOnMethod ends");  
  33.     }  
  34.   
  35.     /** 
  36.      * 对象锁方法2,采用synchronized (this)来加锁 
  37.      */  
  38.     public void synInMethod() {  
  39.         synchronized (this) {  
  40.             System.out.println("synInMethod begins" + ", time = "  
  41.                     + System.currentTimeMillis() + "ms");  
  42.             try {  
  43.                 Thread.sleep(2000L);  
  44.             } catch (InterruptedException e) {  
  45.                 e.printStackTrace();  
  46.             }  
  47.             System.out.println("synInMethod ends");  
  48.         }  
  49.   
  50.     }  
  51.   
  52.     /** 
  53.      * 对象锁方法3 
  54.      */  
  55.     public void synMethodWithObj() {  
  56.         synchronized (object) {  
  57.             System.out.println("synMethodWithObj begins" + ", time = "  
  58.                     + System.currentTimeMillis() + "ms");  
  59.             try {  
  60.                 Thread.sleep(2000L);  
  61.             } catch (InterruptedException e) {  
  62.                 e.printStackTrace();  
  63.             }  
  64.             System.out.println("synMethodWithObj ends");  
  65.         }  
  66.     }  
  67.   
  68.     /** 
  69.      * 类锁 
  70.      */  
  71.     public static synchronized void <span style="font-size: 1em; line-height: 1.5;">increament</span><span style="font-size: 1em; line-height: 1.5;">() {</span>  
  72.         System.out.println("class synchronized. i = " + i + ", time = "  
  73.                 + System.currentTimeMillis() + "ms");  
  74.         i++;  
  75.         try {  
  76.             Thread.sleep(2000L);  
  77.         } catch (InterruptedException e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.          System.out.println("class synchronized ends.");  
  81.     }  
  82.   
  83. }  

    

三、测试结果

   1.测试类锁和对象锁,ObjectThread的run方法修改如下:

 

Java代码  收藏代码
  1.     public void run() {  
  2.         //无锁方法  
  3. //      lock.noSynMethod(this.getId(),this);  
  4.         //对象锁方法1,采用synchronized synInMethod的方式  
  5.         lock.synInMethod();  
  6.         //对象锁方法2,采用synchronized(this)的方式  
  7. //      lock.synOnMethod();  
  8.         //私有锁方法,采用synchronized(object)的方式  
  9. //      lock.synMethodWithObj();  
  10.         //类锁方法,采用static synchronized increment的方式  
  11.         LockTestClass.increament();  
  12.     }  
    终端输出如下:
Java代码  收藏代码
  1. start time = 1413101360231ms  
  2. synInMethod begins, time = 1413101360233ms  
  3. synInMethod ends  
  4. class synchronized. i = 0, time = 1413101362233ms  
  5. synInMethod begins, time = 1413101362233ms  
  6. class synchronized ends.  
  7. synInMethod ends  
  8. class synchronized. i = 1, time = 1413101364233ms  
  9. synInMethod begins, time = 1413101364233ms  
  10. class synchronized ends.  
  11. synInMethod ends  
  12. class synchronized. i = 2, time = 1413101366234ms  
  13. class synchronized ends.  
    可以看到对象锁方法(synInMothod)第一次启动时比类锁方法(increament)快2秒,这是因为在synInMehtod执行时sleep了2秒再执行的increament,而这两个方法共用一个线程,所以会慢2秒,如果increament在run中放到synInMethod前面,那么第一次启动时就是increament快2秒。
   而当类锁方法启动时,另一个线程时的对象锁方法也几乎同时启动,说明二者使用的并非同一个锁,不会产生竞争。
  结论:类锁和对象锁不会产生竞争,二者的加锁方法不会相互影响。
 
  2.私有锁和对象锁,ObjectThread的run方法修改如下:
Java代码  收藏代码
  1. public void run() {  
  2.         //无锁方法  
  3. //      lock.noSynMethod(this.getId(),this);  
  4.         //对象锁方法1,采用synchronized synInMethod的方式  
  5.         lock.synInMethod();  
  6.         //对象锁方法2,采用synchronized(this)的方式  
  7. //      lock.synOnMethod();  
  8.         //私有锁方法,采用synchronized(object)的方式  
  9.         lock.synMethodWithObj();  
  10.         //类锁方法,采用static synchronized increment的方式  
  11. //      LockTestClass.increament();  
  12.     }  
    终端输出如下:
Java代码  收藏代码
  1. start time = 1413121912406ms  
  2. synInMethod begins, time = 1413121912407ms.  
  3. synInMethod ends.  
  4. synMethodWithObj begins, time = 1413121914407ms  
  5. synInMethod begins, time = 1413121914407ms.  
  6. synInMethod ends.  
  7. synMethodWithObj ends  
  8. synInMethod begins, time = 1413121916407ms.  
  9. synMethodWithObj begins, time = 1413121916407ms  
  10. synInMethod ends.  
  11. synMethodWithObj ends  
  12. synMethodWithObj begins, time = 1413121918407ms  
  13. synMethodWithObj ends  

 

    和类锁和对象锁非常类似。
    结论:私有锁和对象锁也不会产生竞争,二者的加锁方法不会相互影响。
 

     3.synchronized直接加在方法上和synchronized(this),ObjectThread的run方法修改如下:

Java代码  收藏代码
  1.     public void run() {  
  2.         //无锁方法  
  3. //      lock.noSynMethod(this.getId(),this);  
  4.         //对象锁方法1,采用synchronized synInMethod的方式  
  5.         lock.synInMethod();  
  6.         //对象锁方法2,采用synchronized(this)的方式  
  7.         lock.synOnMethod();  
  8.         //私有锁方法,采用synchronized(object)的方式  
  9. //      lock.synMethodWithObj();  
  10.         //类锁方法,采用static synchronized increment的方式  
  11. //      LockTestClass.increament();  
  12.     }  
    终端输出:
Java代码  收藏代码
  1. start time = 1413102913278ms  
  2. synInMethod begins, time = 1413102913279ms  
  3. synInMethod ends  
  4. synInMethod begins, time = 1413102915279ms  
  5. synInMethod ends  
  6. synOnMethod begins, time = 1413102917279ms  
  7. synOnMethod ends  
  8. synInMethod begins, time = 1413102919279ms  
  9. synInMethod ends  
  10. synOnMethod begins, time = 1413102921279ms  
  11. synOnMethod ends  
  12. synOnMethod begins, time = 1413102923279ms  
  13. synOnMethod ends  

    可以看到,二者严格地串行输出(当然再次执行时先运行synInMethod还是先运行synOnMethod并不是确定的,取决于谁获得了锁)

    结论:synchronized直接加在方法上和synchronized(this)都是对当前对象加锁,二者的加锁方法够成了竞争关系,同一时刻只能有一个方法能执行。

posted @ 2016-09-23 10:57  Evil_XJZ  阅读(207)  评论(0编辑  收藏  举报