从头认识多线程-2.11 synchronized标记的方法和synchronized(this)标记的代码块锁定的是当前对象
2016-05-05 21:36
519 查看
这一章节我们来讨论一下synchronized标记的方法和synchronized(this)标记的代码块锁定的是当前对象。
1.其实从语句上面我们就已经可以看出synchronized(this)锁定的是当前对象,因为能够使用this的只有指代当前对象(当然,在构造器里面有些this除外)
2.代码清单:
输出:
com.ray.deepintothread.ch02.topic_12.MyService2@9f34d54
Thread name:Thread-0 count:0
Thread name:Thread-0 count:1
Thread name:Thread-0 count:2
Thread name:Thread-0 count:3
Thread name:Thread-0 count:4
com.ray.deepintothread.ch02.topic_12.MyService2@9f34d54
Thread name:Thread-1 count:5
Thread name:Thread-1 count:6
Thread name:Thread-1 count:7
Thread name:Thread-1 count:8
Thread name:Thread-1 count:9
2.结论
看第一个代码清单,synchronized标记的方法,我们其实比较难发现标记的是当前对象,只有在方法里面打印出来才知道,对比两个对象的内存地址,是一致的
看第二个代码清单,因为直接是引用this,因此肯定是当前对象
总结:这一章节展示了synchronized标记的方法和synchronized(this)标记的代码块锁定的是当前对象。
这一章节就到这里,谢谢
------------------------------------------------------------------------------------
我的github:https://github.com/raylee2015/DeepIntoThread
目录:/article/7548544.html
1.其实从语句上面我们就已经可以看出synchronized(this)锁定的是当前对象,因为能够使用this的只有指代当前对象(当然,在构造器里面有些this除外)
2.代码清单:
package com.ray.deepintothread.ch02.topic_12; /** * * @author RayLee * */ public class ObjectLock2 { public static void main(String[] args) throws InterruptedException { MyService2 myService2 = new MyService2(); ThreadThree threadThree = new ThreadThree(myService2); Thread thread = new Thread(threadThree); thread.start(); ThreadFour threadFour = new ThreadFour(myService2); Thread thread2 = new Thread(threadFour); thread2.start(); } } class ThreadThree implements Runnable { private MyService2 myService2; public ThreadThree(MyService2 myService2) { this.myService2 = myService2; } @Override public void run() { try { myService2.updateA(); } catch (InterruptedException e) { e.printStackTrace(); } } } class ThreadFour implements Runnable { private MyService2 myService2; public ThreadFour(MyService2 myService2) { this.myService2 = myService2; } @Override public void run() { try { myService2.updateB(); } catch (InterruptedException e) { e.printStackTrace(); } } } class MyService2 { private int count = 0; public synchronized void updateA() throws InterruptedException { System.out.println(this); for (int i = 0; i < 5; i++) { System.out.println("Thread name:" + Thread.currentThread().getName() + " count:" + count++); Thread.sleep(50); } } public synchronized void updateB() throws InterruptedException { System.out.println(this); for (int i = 0; i < 5; i++) { System.out.println("Thread name:" + Thread.currentThread().getName() + " count:" + count++); Thread.sleep(100); } } }
输出:
com.ray.deepintothread.ch02.topic_12.MyService2@9f34d54
Thread name:Thread-0 count:0
Thread name:Thread-0 count:1
Thread name:Thread-0 count:2
Thread name:Thread-0 count:3
Thread name:Thread-0 count:4
com.ray.deepintothread.ch02.topic_12.MyService2@9f34d54
Thread name:Thread-1 count:5
Thread name:Thread-1 count:6
Thread name:Thread-1 count:7
Thread name:Thread-1 count:8
Thread name:Thread-1 count:9
package com.ray.deepintothread.ch02.topic_12; /** * * @author RayLee * */ public class ObjectLock { public static void main(String[] args) throws InterruptedException { MyService myService = new MyService(); ThreadOne threadOne = new ThreadOne(myService); Thread thread = new Thread(threadOne); thread.start(); ThreadTwo threadTwo = new ThreadTwo(myService); Thread thread2 = new Thread(threadTwo); thread2.start(); } } class ThreadOne implements Runnable { private MyService myService; public ThreadOne(MyService myService) { this.myService = myService; } @Override public void run() { try { myService.updateA(); } catch (InterruptedException e) { e.printStackTrace(); } } } class ThreadTwo implements Runnable { private MyService myService; public ThreadTwo(MyService myService) { this.myService = myService; } @Override public void run() { try { myService.updateB(); } catch (InterruptedException e) { e.printStackTrace(); } } } class MyService { public void updateA() throws InterruptedException { synchronized (this) { long startTime = System.currentTimeMillis(); System.out.println("updateA startTime:" + startTime); Thread.sleep(1000); long endTime = System.currentTimeMillis(); System.out.println("updateA endTime:" + endTime); } } public void updateB() throws InterruptedException { synchronized (this) { long startTime = System.currentTimeMillis(); System.out.println("updateB startTime:" + startTime); Thread.sleep(1000); long endTime = System.currentTimeMillis(); System.out.println("updateB endTime:" + endTime); } } }
2.结论
看第一个代码清单,synchronized标记的方法,我们其实比较难发现标记的是当前对象,只有在方法里面打印出来才知道,对比两个对象的内存地址,是一致的
看第二个代码清单,因为直接是引用this,因此肯定是当前对象
总结:这一章节展示了synchronized标记的方法和synchronized(this)标记的代码块锁定的是当前对象。
这一章节就到这里,谢谢
------------------------------------------------------------------------------------
我的github:https://github.com/raylee2015/DeepIntoThread
目录:/article/7548544.html
相关文章推荐
- Spring Web MVC架构
- python自动化脚本ftp应用
- 基于Netbeans的安卓Android开发环境配置
- More Effective C++----(10)在构造函数中防止资源泄漏
- 从源码理解LinkedList.java
- 【C++】VS2010生成的程序在安装了vc2010运行库的机器上提示“丢失MSVCR100D.dll”
- 安卓开发基础新手必知java知识之接口与抽象类的区别
- C++数组的二分法查找
- C语言中的移位操作
- JavaWeb学习笔记——javabean
- 改变hosts文件来访问Google,facebook,youtube
- 设置python 模块调用路径
- php查询技术之-关键词高亮显示
- 《Windows核心编程》---Interlocked原子访问系列函数
- Java之int与Integer区别
- 面向项目(一) —— 代码编写
- Eclipse中项目Project Explorer视图与Package Explorer视图
- JAVA并发编程实战---第三章:对象的共享(2)
- java的多态
- Vertrigo Serv + testlink 环境搭建