死锁:同步中嵌套同步
package com.hyf.deadLock;
/**
* 死锁程序
* @author Administrator
*
*/
public class DeadLock {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1=new Thread(new Test(true));
Thread t2=new Thread(new Test(false));
t1.start();
t2.start();
}
}
class Test implements Runnable{
private boolean flag;
Test (boolean flag){
this.flag=flag;
}
@Override
public void run() {
// TODO Auto-generated method stub
if(flag) {
while(true){
synchronized(MyLock.locka){
System.out.println("if locka");
synchronized(MyLock.lockb){
System.out.println("if lockb");
}
}
}
}else {
while(true){
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();
}