ps:不是阻塞状态,如wait()不被notify唤醒时是阻塞
必须在两个及两个以上对象锁时才会发生。两个人过独木桥的例子!!
public class N extends Thread
{
public static Object o1=new Object();
public static Object o2=new Object();
public static void main(String[] args)
{
new N().start();
synchronized(o1){
System.out.println(1);
try{
Thread.sleep(1);
}catch(InterruptedException e){
e.printStackTrace();
}
synchronized(o2){
System.out.println(2);
}
}
}
public void run(){
synchronized(o2){
System.out.println(3);
try{
Thread.sleep(1);
}catch(InterruptedException e){
e.printStackTrace();
}
synchronized(o1){
System.out.println(4);
}
}
}
}
打印:
1
3
锁死