这个笔试的话频率也挺高(遇见笔试的公司要三思啊),所以这里直接给出一个答案(有很多版本的)。
public class DeadLockDemo {
public static void main(String[] args){
Object objectl = new Object();
Object object2 = new Object();
Thread t1= new Thread(()->{
synchronized (object1){
try{
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}
synchronized (object2){
}
}
},"deadlock-demo-1");
t1.start();
Thread t2 = new Thread(()-> {
synchronized (object2){
synchronized (object1) {
}
}
},."deadlock-demo-2");
t2.start();
}
}
Was this helpful?
0 / 0