从头认识多线程-2.3 synchronized持有对象锁与类锁的不同点
来源:互联网 发布:地基处理计算软件 编辑:程序博客网 时间:2024/05/18 23:55
这一章节我们来讨论游戏,synchronized持有对象锁与类锁的不同点-两种锁是并行的东西,没有交集。
1.同步持有对象锁或者类锁
package com.ray.deepintothread.ch02.topic_3;public class SynchInstance5 {public static void main(String[] args) throws InterruptedException {MyTestObjectFive myTestObjectFive = new MyTestObjectFive();for (int i = 0; i < 2; i++) {ThreadFive threadFive = new ThreadFive(myTestObjectFive);Thread thread = new Thread(threadFive);thread.setName("" + i);thread.start();}}}class ThreadFive implements Runnable {private MyTestObjectFive myTestObjectFive;public ThreadFive(MyTestObjectFive myTestObjectFive) {this.myTestObjectFive = myTestObjectFive;}@Overridepublic void run() {try {if (Integer.parseInt(Thread.currentThread().getName()) % 2 == 0) {myTestObjectFive.test1();} else {myTestObjectFive.test2();}} catch (InterruptedException e) {e.printStackTrace();}}}class MyTestObjectFive {public synchronized void test1() throws InterruptedException {for (int i = 0; i < 5; i++) {System.out.println(i);Thread.sleep(100);}}public synchronized void test2() throws InterruptedException {for (int i = 0; i < 5; i++) {System.out.println(i);Thread.sleep(100);}}}
输出:
0
1
2
3
4
0
1
2
3
4
package com.ray.deepintothread.ch02.topic_3;public class SynchInstance6 {public static void main(String[] args) throws InterruptedException {for (int i = 0; i < 2; i++) {ThreadSix threadSix = new ThreadSix();Thread thread = new Thread(threadSix);thread.setName("" + i);thread.start();}}}class ThreadSix implements Runnable {@Overridepublic void run() {try {if (Integer.parseInt(Thread.currentThread().getName()) % 2 == 0) {MyTestObjectSix.test1();} else {MyTestObjectSix.test2();}} catch (InterruptedException e) {e.printStackTrace();}}}class MyTestObjectSix {public static synchronized void test1() throws InterruptedException {for (int i = 0; i < 5; i++) {System.out.println(i);Thread.sleep(100);}}public static synchronized void test2() throws InterruptedException {for (int i = 0; i < 5; i++) {System.out.println(i);Thread.sleep(100);}}}
输出:
0
1
2
3
4
0
1
2
3
4
像前面章节一样,当我们同步持有对象锁或者类锁,输出的是按照顺序的
2.当同时持有对象锁和类锁
package com.ray.deepintothread.ch02.topic_3;public class SynchInstance4 {public static void main(String[] args) throws InterruptedException {MyTestObjectFour myTestObjectThree = new MyTestObjectFour();for (int i = 0; i < 2; i++) {ThreadFour threadFour = new ThreadFour(myTestObjectThree);Thread thread = new Thread(threadFour);thread.setName("" + i);thread.start();}}}class ThreadFour implements Runnable {private MyTestObjectFour myTestObjectThree;public ThreadFour(MyTestObjectFour myTestObjectThree) {this.myTestObjectThree = myTestObjectThree;}@Overridepublic void run() {try {if (Integer.parseInt(Thread.currentThread().getName()) % 2 == 0) {myTestObjectThree.test1();} else {MyTestObjectFour.test2();}} catch (InterruptedException e) {e.printStackTrace();}}}class MyTestObjectFour {public synchronized void test1() throws InterruptedException {for (int i = 0; i < 5; i++) {System.out.println(i);Thread.sleep(100);}}public static synchronized void test2() throws InterruptedException {for (int i = 0; i < 5; i++) {System.out.println(i);Thread.sleep(100);}}}
输出:
0
0
1
1
2
2
3
3
4
4
从输出可以看见,两者就像平行的关系,没有交集,交替的出现。但也是有秩序的。
总结:这一章节讨论了synchronized持有对象锁与类锁的不同点
这一章节就到这里,谢谢
------------------------------------------------------------------------------------
我的github:https://github.com/raylee2015/DeepIntoThread
目录:http://blog.csdn.net/raylee2007/article/details/51204573
0 0
- 从头认识多线程-2.3 synchronized持有对象锁与类锁的不同点
- 从头认识多线程-2.2 synchronized持有对象锁与类锁的相同点
- 从头认识多线程-2.11 通过同步代码块证明synchronized标记的是对象锁
- 从头认识多线程-2.5 锁的可重入性
- 从头认识多线程-2.20 synchronized同步方法的无限等待与解决方法
- 多线程锁的重复持有
- 从头认识多线程-2.18 同步方法与同步静态代码块持有的是不同的锁
- 从头认识多线程-2.12 synchronized标记的方法和synchronized(this)标记的代码块锁定的是当前对象
- 从头认识多线程-2.13 synchronized ()代码块不单可以用this,也可以用其他对象
- 多线程并发 synchronized对象锁的控制与优化
- 多线程并发 synchronized对象锁的控制与优化
- 从头认识多线程-2.15 解决由同步的synchronized (newobject()) 引起的脏读的方法
- 从头认识多线程-2.14 由同步的synchronized (newobject()) 引起的异步现象和脏读
- 从头认识多线程-2.25 synchronized同步方法在jvm是怎样执行的?
- 从头认识多线程-2.26 synchronized同步代码块在jvm是怎样执行的?
- 从头认识多线程-3.3 synchronized某些解决不了的可视性问题,只能使用volatile来解决
- 从头认识多线程-1.1 多线程的创建
- Oracle 查看 对象 持有 锁 的情况
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
- AFNetworking 2.x 的SSL身份认证
- MySql乱码解决
- COM口 大于9 连接不上问题总结
- 关于Mac下的Android Studio无法显示类的相关文档和关联源代码的问题
- 从头认识多线程-2.3 synchronized持有对象锁与类锁的不同点
- 简单聊一下Android 6.0的运行时权限
- fedora 一命令安装 maven
- TCP/IP SOCKET HTTP及HTTPS之间的关系及各自特性之总结
- Android面试题——Android四大组件
- 获取文件上传进度
- iOS开发之--图形上下文以及绘图
- Centos6.5编译php报错:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
- Android 风格与主题(style and theme)