




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
对synchronized(this)的一些理解一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。五、以上规则对其它对象锁同样适用.举例说明:一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。package ths;public class Thread1 implements Runnable public void run() synchronized(this) for (int i = 0; i 0) System.out.println(Thread.currentThread().getName() + : + i);try Thread.sleep(500); catch (InterruptedException ie) public void m4t2() int i = 5;while( i- 0) System.out.println(Thread.currentThread().getName() + : + i);try Thread.sleep(500); catch (InterruptedException ie) public static void main(String args) final Thread2 myt2 = new Thread2();Thread t1 = new Thread(new Runnable() public void run() myt2.m4t1();, t1);Thread t2 = new Thread(new Runnable() public void run() myt2.m4t2();, t2);t1.start();t2.start();结果:t1 : 4t2 : 4t1 : 3t2 : 3t1 : 2t2 : 2t1 : 1t2 : 1t1 : 0t2 : 0三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。/修改Thread2.m4t2()方法:public void m4t2() synchronized(this) int i = 5;while( i- 0) System.out.println(Thread.currentThread().getName() + : + i);try Thread.sleep(500); catch (InterruptedException ie) 结果:t1 : 4t1 : 3t1 : 2t1 : 1t1 : 0t2 : 4t2 : 3t2 : 2t2 : 1t2 : 0四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。/修改Thread2.m4t2()方法如下:public synchronized void m4t2() int i = 5;while( i- 0) System.out.println(Thread.currentThread().getName() + : + i);try Thread.sleep(500); catch (InterruptedException ie) 结果:t1 : 4t1 : 3t1 : 2t1 : 1t1 : 0t2 : 4t2 : 3t2 : 2t2 : 1t2 : 0五、以上规则对其它对象锁同样适用:package ths;public class Thread3 class Inner private void m4t1() int i = 5;while(i- 0) System.out.println(Thread.currentThread().getName() + : Inner.m4t1()= + i);try Thread.sleep(500); catch(InterruptedException ie) private void m4t2() int i = 5;while(i- 0) System.out.println(Thread.currentThread().getName() + : Inner.m4t2()= + i);try Thread.sleep(500); catch(InterruptedException ie) private void m4t1(Inner inner) synchronized(inner) /使用对象锁inner.m4t1();private void m4t2(Inner inner) inner.m4t2();public static void main(String args) final Thread3 myt3 = new Thread3();final Inner inner = myt3.new Inner();Thread t1 = new Thread(new Runnable() public void run() myt3.m4t1(inner);, t1);Thread t2 = new Thread(new Runnable() public void run() myt3.m4t2(inner);, t2);t1.start();t2.start();结果:尽管线程t1获得了对Inner的对象锁,但由于线程t2访问的是同一个Inner中的非同步部分。所以两个线程互不干扰。t1 : Inner.m4t1()=4t2 : Inner.m4t2()=4t1 : Inner.m4t1()=3t2 : Inner.m4t2()=3t1 : Inner.m4t1()=2t2 : Inner.m4t2()=2t1 : Inner.m4t1()=1t2 : Inner.m4t2()=1t1 : Inner.m4t1()=0t2 : Inner.m4t2()=0现在在Inner.m4t2()前面加上synchronized:private synchronized void m4t2() int i = 5;while(i- 0) System.out.println(Thread.currentThread().getName() + : Inner.m4t2()= + i);try Thread.sleep(500); catch(InterruptedException ie) 结果:尽管线程t1与t2访问了同一个Inner对象中两个毫不相关的部分,但因为t1先获得了对Inner的对象锁,所以t2对Inner.m4t2()的访问也被阻塞,因为m4t2()是Inner中的一个同步方法。t1 : Inner.m4
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安全培训效率课件
- Glycoisolithocholanoic-acid-生命科学试剂-MCE
- Glucoraphanin-sodium-d5-生命科学试剂-MCE
- 2025广东广州市中山大学孙逸仙纪念医院超声科医教研岗位招聘模拟试卷及答案详解(名师系列)
- 2025广东深圳市九洲电器有限公司关IQC招聘1人模拟试卷及答案详解(网校专用)
- 2025广东惠州龙门县教育局招聘教师80人考前自测高频考点模拟试题附答案详解(典型题)
- 2025河南许昌市建安区招聘公益性岗位人员13人模拟试卷及1套参考答案详解
- 2025广西柳州市城中区委社会工作部招聘专职化城市社区工作者5人模拟试卷及答案详解(有一套)
- 项目管理进度跟踪表标准化流程控制
- 经济合同范本
- GB/T 6478-2015冷镦和冷挤压用钢
- GB/T 11376-2020金属及其他无机覆盖层金属的磷化膜
- 2022年《体育法》全文PPT
- 公司区域经营管理办法
- 五年级《中国民间故事》知识考试题库(含答案)
- 《景观生态设计》课件
- 江苏省南通市各县区乡镇行政村村庄村名居民村民委员会明细
- 中国古典乐器-古筝琵琶英文介绍(带翻译)课件
- 地表形态塑造内力作用说课
- QSY02553-2018井下作业井控技术规范
- 苏教版(新教材)三年级上册小学科学第二单元测试卷含答案
评论
0/150
提交评论