________________死锁
//简单易懂的死锁程序
public class Lock implements Runnable {
Boolean flag;
public Lock(boolean flag){
this.flag=flag;
}
@Override
public void run() {
while(true){
if(flag)
{
synchronized(Mylock.locka){
yield();
System.out.println(Thread.currentThread()
.getName()+"if__________locka");
synchronized(Mylock.lockb){
System.out.println(Thread.currentThread()
.getName()+"if__________lockb");
}
}
}
else{
synchronized(Mylock.lockb){
yield();
System.out.println(Thread.currentThread()
.getName()+"else__________locka");
synchronized(Mylock.locka){
System.out.println(Thread.currentThread()
.getName()+"else__________lockb");
}
}
}
}
}
private void yield() {
// TODO Auto-generated method stub
}
static class Mylock{
public static final Object locka=new Object();
public static final Object lockb=new Object();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Lock a=new Lock(false);
Lock b=new Lock(true);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步