当前位置: 移动技术网 > IT编程>开发语言>Java > Java类锁、对象锁、私有锁冲突测试

Java类锁、对象锁、私有锁冲突测试

2019年07月22日  | 移动技术网IT编程  | 我要评论

类锁和对象锁是否会冲突?对象锁和私有锁是否会冲突?通过实例来进行说明。

一、相关约定

为了明确后文的描述,先对本文涉及到的锁的相关定义作如下约定:

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

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

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

二、测试代码

1.编写一个启动类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.编写一个线程类objthread,用于启动同步方法(注意它的run方法可能会调整以进行不同的测试)

复制代码 代码如下:

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);
  //对象锁方法1,采用synchronized syninmethod的方式
  lock.syninmethod();
  //对象锁方法2,采用synchronized(this)的方式
//  lock.synonmethod();
  //私有锁方法,采用synchronized(object)的方式
//  lock.synmethodwithobj();
  //类锁方法,采用static synchronized increment的方式
  locktestclass.increment();
 }
}

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

复制代码 代码如下:

public class locktestclass {
 //用于类锁计数
 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);
 }

 /**
  * 对象锁方法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");
 }

 /**
  * 对象锁方法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");
  }

 }

 /**
  * 对象锁方法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.");
 }

}

三、测试结果

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

复制代码 代码如下:

public void run() {
  //无锁方法
//  lock.nosynmethod(this.getid(),this);
  //对象锁方法1,采用synchronized syninmethod的方式
  lock.syninmethod();
  //对象锁方法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.

可以看到对象锁方法(syninmothod)第一次启动时比类锁方法(increament)快2秒,这是因为在syninmehtod执行时sleep了2秒再执行的increament,而这两个方法共用一个线程,所以会慢2秒,如果increament在run中放到syninmethod前面,那么第一次启动时就是increament快2秒。

而当类锁方法启动时,另一个线程时的对象锁方法也几乎同时启动,说明二者使用的并非同一个锁,不会产生竞争。

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

2.私有锁和对象锁,objectthread的run方法修改如下:

复制代码 代码如下:

public void run() {
  //无锁方法
//  lock.nosynmethod(this.getid(),this);
  //对象锁方法1,采用synchronized syninmethod的方式
  lock.syninmethod();
  //对象锁方法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

和类锁和对象锁非常类似。

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

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

复制代码 代码如下:

public void run() {
  //无锁方法
//  lock.nosynmethod(this.getid(),this);
  //对象锁方法1,采用synchronized syninmethod的方式
  lock.syninmethod();
  //对象锁方法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

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网