Java并发之死锁实例
2016-10-21 17:12
281 查看
package com.thread.test.thread; /** * Created by windwant on 2016/6/3. */ public class MyTestDeadLock { public void run() { MyThread mt = new MyThread(); new Thread(mt, "zhangsan").start(); new Thread(mt, "lisi").start(); } class MyThread implements Runnable { private Object o1 = new Object(); private Object o2 = new Object(); private boolean flag = true; public void run() { if (flag) { flag = false; synchronized (o1) { System.out.println(Thread.currentThread().getName() + " have o1"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { System.out.println(Thread.currentThread().getName() + " have o2"); } } } else { flag = true; synchronized (o2) { System.out.println(Thread.currentThread().getName() + " have o2"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1) { System.out.println(Thread.currentThread().getName() + " have o1"); } } } } } public static void main(String[] args) { new MyTestDeadLock().run(); } }
项目地址:https://github.com/windwant/threadtest
相关文章推荐
- Java线程:并发协作-死锁
- Java线程:并发协作-死锁(图)
- Java 线程与并发研究系列六(死锁)
- Java并发(具体实例)—— 构建高效且可伸缩的结果缓存
- Java 并发编程之死锁
- Java线程:并发协作-死锁
- Java并发编程4.2-实例限制
- Java线程:并发协作-死锁
- Java线程:并发协作-死锁
- Java并发(具体实例)——几个例子
- 构建高性能服务(二)减小锁粒度 提高Java并发吞吐实例
- java并发之固定对象与实例
- 【翻译十五】-java并发之固定对象与实例
- Java并发学习笔记(12)避免活跃度危险(死锁)
- 【Java并发编程】之九:死锁(含代码)
- Java并发集合类实例
- Java 编程下的并发线程之间的同步代码块死锁
- java readwritelock 非死锁 实例 (对比上文)
- Java线程:并发协作-死锁
- Java并发(具体实例)——构建高效且可伸缩的结果缓存