立即学习:https://edu.csdn.net/course/play/9827/208780?utm_source=blogtoedu
public class test1 {
private static final Object hair_a=new Object();
private static final Object hair_b=new Object();
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
synchronized (hair_a) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (hair_b) {
System.out.println("a抓到了b");
}
}
}
});
Thread thread2 = new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
synchronized (hair_b) {
synchronized (hair_a) {
System.out.println("b抓到了a");
}
}
}
});
thread1.start();
thread2.start();
}
}