(转)java并发对象锁、类锁、私有锁
转自:http://ifeve.com/java-locks/
建议参考:http://www.zhihu.com/question/28113814
Java类锁和对象锁实践
感谢[jiehao]同学的投稿,投稿可将文章发送到tengfei@ifeve.com
类锁和对象锁是否会冲突?对象锁和私有锁是否会冲突?通过实例来进行说明。
一、相关约定
为了明确后文的描述,先对本文涉及到的锁的相关定义作如下约定:
1. 类锁:在代码中的方法上加了static和synchronized的锁,或者synchronized(xxx.class)的代码段,如下文中的increament();
2.对象锁:在代码中的方法上加了synchronized的锁,或者synchronized(this)的代码段,如下文中的synOnMethod()和synInMethod();
3.私有锁:在类内部声明一个私有属性如private Object lock,在需要加锁的代码段synchronized(lock),如下文中的synMethodWithObj()。
二、测试代码
1.编写一个启动类ObjectLock
01 |
public class ObjectLock { |
02 |
public static void main(String[] args) { |
03 |
System.out.println("start time = " + System.currentTimeMillis()+"ms"); |
04 |
LockTestClass test = new LockTestClass(); |
05 |
for (int i = 0; i < 3; i++) { |
06 |
Thread thread = new ObjThread(test, i); |
07 |
thread.start(); |
08 |
} |
09 |
} |
10 |
} |
2.编写一个线程类ObjThread,用于启动同步方法(注意它的run方法可能会调整以进行不同的测试)
01 |
public class ObjThread extends Thread { |
02 |
LockTestClass lock; |
03 |
int i = 0; |
04 |
05 |
public ObjThread(LockTestClass lock, int i) { |
06 |
this.lock = lock; |
07 |
this.i = i; |
08 |
} |
09 |
10 |
public void run() { |
11 |
//无锁方法 |
12 |
// lock.noSynMethod(this.getId(),this); |
13 |
//对象锁方法1,采用synchronized synInMethod的方式 |
14 |
lock.synInMethod(); |
15 |
//对象锁方法2,采用synchronized(this)的方式 |
16 |
// lock.synOnMethod(); |
17 |
//私有锁方法,采用synchronized(object)的方式 |
18 |
// lock.synMethodWithObj(); |
19 |
//类锁方法,采用static synchronized increment的方式 |
20 |
LockTestClass.increment(); |
21 |
} |
22 |
} |
3.再编写一个锁的测试类LockTestClass,包括各种加锁方法
01 |
public class LockTestClass { |
02 |
//用于类锁计数 |
03 |
private static int i = 0; |
04 |
//私有锁 |
05 |
private Object object = new Object(); |
06 |
07 |
/** |
08 |
* <p> |
09 |
* 无锁方法 |
10 |
* |
11 |
* @param threadID |
12 |
* @param thread |
13 |
*/ |
14 |
public void noSynMethod(long threadID, ObjThread thread) { |
15 |
System.out.println("nosyn: class obj is " + thread + ", threadId is" |
16 |
+ threadID); |
17 |
} |
18 |
19 |
/** |
20 |
* 对象锁方法1 |
21 |
*/ |
22 |
public synchronized void synOnMethod() { |
23 |
System.out.println("synOnMethod begins" + ", time = " |
24 |
+ System.currentTimeMillis() + "ms"); |
25 |
try { |
26 |
Thread.sleep(2000L); |
27 |
} catch (InterruptedException e) { |
28 |
e.printStackTrace(); |
29 |
} |
30 |
System.out.println("synOnMethod ends"); |
31 |
} |
32 |
33 |
/** |
34 |
* 对象锁方法2,采用synchronized (this)来加锁 |
35 |
*/ |
36 |
public void synInMethod() { |
37 |
synchronized (this) { |
38 |
System.out.println("synInMethod begins" + ", time = " |
39 |
+ System.currentTimeMillis() + "ms"); |
40 |
try { |
41 |
Thread.sleep(2000L); |
42 |
} catch (InterruptedException e) { |
43 |
e.printStackTrace(); |
44 |
} |
45 |
System.out.println("synInMethod ends"); |
46 |
} |
47 |
48 |
} |
49 |
50 |
/** |
51 |
* 对象锁方法3 |
52 |
*/ |
53 |
public void synMethodWithObj() { |
54 |
synchronized (object) { |
55 |
System.out.println("synMethodWithObj begins" + ", time = " |
56 |
+ System.currentTimeMillis() + "ms"); |
57 |
try { |
58 |
Thread.sleep(2000L); |
59 |
} catch (InterruptedException e) { |
60 |
e.printStackTrace(); |
61 |
} |
62 |
System.out.println("synMethodWithObj ends"); |
63 |
} |
64 |
} |
65 |
66 |
/** |
67 |
* 类锁 |
68 |
*/ |
69 |
public static synchronized void increament() { |
70 |
System.out.println("class synchronized. i = " + i + ", time = " |
71 |
+ System.currentTimeMillis() + "ms"); |
72 |
i++; |
73 |
try { |
74 |
Thread.sleep(2000L); |
75 |
} catch (InterruptedException e) { |
76 |
e.printStackTrace(); |
77 |
} |
78 |
System.out.println("class synchronized ends."); |
79 |
} |
80 |
81 |
} |
三、测试结果
1.测试类锁和对象锁,ObjectThread的run方法修改如下:
01 |
public void run() { |
02 |
//无锁方法 |
03 |
// lock.noSynMethod(this.getId(),this); |
04 |
//对象锁方法1,采用synchronized synInMethod的方式 |
05 |
lock.synInMethod(); |
06 |
//对象锁方法2,采用synchronized(this)的方式 |
07 |
// lock.synOnMethod(); |
08 |
//私有锁方法,采用synchronized(object)的方式 |
09 |
// lock.synMethodWithObj(); |
10 |
//类锁方法,采用static synchronized increment的方式 |
11 |
LockTestClass.increament(); |
12 |
} |
终端输出:
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方法修改如下:
01 |
public void run() { |
02 |
//无锁方法 |
03 |
// lock.noSynMethod(this.getId(),this); |
04 |
//对象锁方法1,采用synchronized synInMethod的方式 |
05 |
lock.synInMethod(); |
06 |
//对象锁方法2,采用synchronized(this)的方式 |
07 |
// lock.synOnMethod(); |
08 |
//私有锁方法,采用synchronized(object)的方式 |
09 |
lock.synMethodWithObj(); |
10 |
//类锁方法,采用static synchronized increment的方式 |
11 |
// LockTestClass.increament(); |
12 |
} |
终端输出:
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
和类锁和对象锁非常类似。
结论:私有锁和对象锁也不会产生竞争,二者的加锁方法不会相互影响。
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)都是对当前对象加锁,二者的加锁方法够成了竞争关系,同一时刻只能有一个方法能执行。
四、参考资料:
原创文章,转载请注明: 转载自并发编程网 – ifeve.com
(转)java并发对象锁、类锁、私有锁的更多相关文章
- Java并发编程系列-(4) 显式锁与AQS
4 显示锁和AQS 4.1 Lock接口 核心方法 Java在java.util.concurrent.locks包中提供了一系列的显示锁类,其中最基础的就是Lock接口,该接口提供了几个常见的锁相关 ...
- Java常见对象Object类中的个别方法
Java常见对象Object类 public int hashCode() : 返回该对象的哈希码值. 注意:哈希值是根据哈希算法计算出来的一个值,这个值和地址值有关,但是不是实际地址值.你可以理解成 ...
- 快乐编程大本营【java语言训练班】 6课:用java的对象和类编程
快乐编程大本营[java语言训练班] 6课:用java的对象和类编程 第1节. 什么是对象和类 第2节. 对象的属性和方法 第3节. 类的继承 第4节. 使用举例:创建类,定义方法,定义属性 第5节. ...
- Java并发编程工具类 CountDownLatch CyclicBarrier Semaphore使用Demo
Java并发编程工具类 CountDownLatch CyclicBarrier Semaphore使用Demo CountDownLatch countDownLatch这个类使一个线程等待其他线程 ...
- Java的对象与类,继承
Java的对象与类,继承 题目1.Java类的建立与使用 设计一个用来描述汽车的类,使用类的非静态成员变量来表示汽车的车主姓名.当前的速率和当前方向盘的转向角度,使用类的非静态成员方法来表示改变汽车的 ...
- 你真的了解JAVA中对象和类、this、super和static关键字吗
作者:小牛呼噜噜 | https://xiaoniuhululu.com 计算机内功.JAVA底层.面试相关资料等更多精彩文章在公众号「小牛呼噜噜 」 目录 Java对象究竟是什么? 创建对象的过程 ...
- Java并发(基础知识)——显示锁和同步工具类
显示锁 Lock接口是Java ...
- [转载] java并发编程:Lock(线程锁)
作者:海子 原文链接: http://www.cnblogs.com/dolphin0520/p/3923167.html 出处:http://www.cnblogs.com/dolphin0520/ ...
- java并发编程:线程同步和锁
一.锁的原理 java中每个对象都有一个内置锁.当程序运行到非静态的synchronized同步方法上时,自动获得与正在执行代码类的当前实例(this)有关的锁.获得一个对象的锁也称为获取锁,当程序运 ...
随机推荐
- tomcat中server.xml配置详解(转载)(一)
转载自:https://www.cnblogs.com/starhu/p/5599773.html tomcat中server.xml配置详解 Tomcat Server的结构图如下:(该文件描述了如 ...
- 【Firefly API文档】—— Package DBentrust
http://bbs.gameres.com/thread_219653_1_1.html package dbentrust 该包下面主要是数据库的处理与memcached存储.里面封装了,从mem ...
- unity3d中布娃娃系统
原地址:http://blog.csdn.net/pizi0475/article/details/9771941 转自: http://forum.mirax.com.tw/unity/viewto ...
- [C/C++]关于C++11中的std::move和std::forward
http://www.cnblogs.com/cbscan/archive/2012/01/10/2318482.html http://blog.csdn.net/fcryuuhou/article ...
- 体验了把做HR的感觉,上午看了40份简历,说说感受
原文链接:http://huachichi.info/2013/06/26/experience-of-being-a-hr/ 这两天准备从IBM离职,不要问我为什么要在这么bug的时间离职,总之 ...
- 微信小程序挑一挑辅助
1.窗体 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;usi ...
- 以登录实例简单介绍Servlet使用
1.简单介绍 Java Servlet 是执行在 Web server或应用server上的程序,使用 Servlet.您能够收集来自网页表单的用户输入.呈现来自数据库或者其它源的记录.还能够动态创建 ...
- .NET面试题(三)
第1讲:面试前期准备 1.了解相关技术职务需要的技术人才 2.准备一份出色的个人简历 第2讲:面试前期准备 ...
- LayerMask小结
layerMask参数: Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerM ...
- 多线程-synchronized
引言 synchronized是Java线程同步中的一个重要的概念,synchronized是独占锁(互斥锁),同时也是可重入锁(可重入锁一定程度上避免了死锁的问题,内部是关联一个计数器,加一次锁计数 ...