/*
多线程卖票
*/
class Aa{
public void get(){
System.out.println("我已经或得A,把B给我吧!");
}
public void say(){
System.out.println("我已经或得B!");
}
}
class B{
public void get(){
System.out.println("我已经或得B,把A给我吧!");
}
public void say(){
System.out.println("我已经或得A!");
}
}
class Al implements Runnable{
Aa a=new Aa();
B b=new B();
public boolean flag=true;
public void run(){
if(flag){
synchronized(a){
a.get();
try{Thread.sleep(100);} catch(InterruptedException e){}
synchronized(b){
a.say();
}
}
}else{
synchronized(b){
b.get();
try{Thread.sleep(100);} catch(InterruptedException e){}
synchronized(a){
b.say();
}
}
}
}
}
public class A{
public static void main(String[] args)
{
Al obj=new Al();
new Thread(obj).start();
try{Thread.sleep(100);} catch(InterruptedException e){}
obj.flag=false;
new Thread(obj).start();
//new Thread(obj).start();
//new Thread(obj).start();
}
}
java 死锁
最新推荐文章于 2025-02-03 10:08:31 发布