黑马程序员技术交流社区
标题:
Java基础学习日记 - 死锁
[打印本页]
作者:
qihuan
时间:
2015-7-6 21:13
标题:
Java基础学习日记 - 死锁
package practice;
/**
* 死锁程序
* @author Qihuan
*
*/
class DeadLock implements Runnable{
private boolean flag;
public DeadLock(boolean flag) {
// TODO Auto-generated constructor stub
this.flag = flag;
}
@Override
public void run() {
// TODO Auto-generated method stub
if (flag) {
synchronized (MyLock.lockA) {
System.out.println("if lockA");
synchronized (MyLock.lockB) {
System.out.println("if lockB");
}
}
}else{
synchronized (MyLock.lockB) {
System.out.println("else lockB");
synchronized (MyLock.lockA) {
System.out.println("else lockA");
}
}
}
}
}
class MyLock {
static Object lockA = new Object();
static Object lockB = new Object();
}
public class DeadLockTest {
public static void main(String[] args) {
Thread t = new Thread(new DeadLock(true));
Thread t2 = new Thread(new DeadLock(false));
t.start();
t2.start();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2