您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: