package test;
class DeadLock implements Runnable {
public boolean flag = true;
static Object o1 = new Object(), o2 = new Object();
@Override
public void run() {
System.out.println(Thread.currentThread());
System.out.println("flag = " + flag);
if(flag) {
System.out.println("&&&&&&&&&&&&&&&&&");
synchronized(o1) {
try
{
System.out.println("sleep1");
Thread.sleep(500);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("-----下面的没有执行到------");
//下面的没有执行到
synchronized(o2) {
System.out.println("1");
}
System.out.println("<<<<<<<<<<<<<");
}
}
else {
System.out.println("^^^^^^^^^^^^^^^^^^^^^");
synchronized(o2)
{
try {
System.out.println("sleep2");
Thread.sleep(500);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("+++++下面的没有执行到++++++");
// 下面的没有执行到
synchronized(o1) {
System.out.println(“0”);
}
System.out.println(“>>>>>>>>>>>>”);
}
}
}
public static void main(String[] args) {
DeadLock td1 = new DeadLock();
DeadLock td2 = new DeadLock();
td1.flag = true;
td2.flag = false;
new Thread(td1).start();
new Thread(td2).start();
}
}
运行的结果是线程一直处于运行状态,但是被同步的两个代码块的
代码都没有执行.