首页 新闻 会员 周边 捐助

java死锁问题

0
[待解决问题]

package my;

public class dwdwa {

public static void main(String[] args)throws Exception{
    DeadLockThread d1=new DeadLockThread(true);
    DeadLockThread d2=new DeadLockThread(false);
    new Thread(d1,"chinese").start();
    new Thread(d2,"American").start();
    
}

}
class DeadLockThread implements Runnable{
Object chopsticks=new Object();
Object knifeAndFork=new Object();
private boolean flag;
public DeadLockThread(boolean flag)
{
this.flag=flag;
}
public void run() {
if(flag)
{
while(true) {
synchronized(chopsticks)
{
System.out.println(Thread.currentThread().getName()+"----if----chopsticks");
/try {
Thread.sleep(100);
}catch(Exception e) {
e.printStackTrace();
}
/
synchronized(knifeAndFork)
{
System.out.println(Thread.currentThread().getName()+"-----if-----knifeAndFork");
}
}
}
}else {
while(true) {
synchronized(knifeAndFork)
{

                System.out.println(Thread.currentThread().getName()+"-----else----knifeAndFork");
                
                synchronized(chopsticks)
                {
                    System.out.println(Thread.currentThread().getName()+"-----else----chopsticks");
                }
            }
        }
    }
}

}
为什么死锁不了啊?求大神解释!!!

老萌新51的主页 老萌新51 | 菜鸟二级 | 园豆:210
提问于:2020-01-24 14:50
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册