线程初步
2015-09-07 15:25
190 查看
package com.test;
public class DeadLock {
}
package com.test;
public class MyDeadLock implements Runnable{
}
package com.test;
public class TestDead {
}
public class DeadLock {
String s1="我是S1"; String s2="我是S2"; int n = 1; public void write(){ synchronized (s1) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (s2) { s1 = "修改s1----"+n+"次"; System.out.println(s1); } } n++; } public void read(){ synchronized (s2) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (s1) { s2 = "修改s2---"+n+"次"; System.out.println(s2); } } n++; }
}
package com.test;
public class MyDeadLock implements Runnable{
private DeadLock dl; public MyDeadLock(DeadLock dl){ this.dl = dl; } @Override public void run() { for (int i = 0; i <10; i++) { dl.read(); dl.write(); } }
}
package com.test;
public class TestDead {
public static void main(String[] args) { DeadLock dd = new DeadLock(); MyDeadLock md = new MyDeadLock(dd); new Thread(md).start(); new Thread(md).start(); }
}
相关文章推荐
- 动画特效十二:发射器效果
- rmmod: chdir(/lib/modules): No such file or directory 解决方法 (转)
- vim 配置插件
- 静态库与动态库的区别?
- 持续集成是什么
- 扩展Django:实现自己的manage命令
- ConcurrentHashMap、CopyOnWriteArrayList的简要总结:
- Business Model Generation《商业模式新生代》对软件测试的启发
- 字符串最后一个单词的长度
- Objective-C中的@property和@synthesize用法
- linux下给文件夹或者目录赋权
- openssl的CRYPTO_set_locking_callback
- 软件测试的艺术 01 一次自评测试
- CentOS6.5中修改yum源
- C/C++ 多线程入门
- java发送邮件的实现
- 安装plsql developer 11
- 线程之取款
- iTem2
- CALayer和UIView的区别