public class test{
public static void main(String[]args)
{
//可用位置
List<Integer> available =new ArrayList<Integer>();
available.add(1);
available.add(2);
available.add(3);
available.add(4);
available.add(5);
available.add(6);
available.add(7);
//顾客位置
List<Integer> seats1=new ArrayList<Integer>();
seats1.add(1);
seats1.add(2);
List<Integer> seats2=new ArrayList<Integer>();
seats2.add(4);
seats2.add(5);
seats2.add(6);
Cinema c=new Cinema(available,"happy");
new Thread(new customer(c,seats1),"me").start();
new Thread(new customer(c,seats2),"she").start();
}
}
class Cinema{
List<Integer> available; //可用位置
String name;
public Cinema(List<Integer> available,String name)
{
this.available=available;
this.name=name;
}
public boolean bookticks(List<Integer> seats)
{
System.out.println("可用位置:"+available);
List<Integer> copy=new ArrayList<Integer>();
copy.addAll(available);
//去掉两个容器相同的元素
copy.removeAll(seats);
//判断是否选位置成功
if(available.size()-copy.size()!=seats.size())
{
return false;
}
else
{
available=copy;
}
return true;
}
}
class customer implements Runnable{
Cinema cinema; //哪个影院
List<Integer>seats; //多少个位置
public customer(Cinema cinema,List<Integer> seats)
{
this.cinema=cinema;
this.seats=seats;
}
public void run()
{
synchronized(cinema) { //锁影院
boolean flag=cinema.bookticks(seats);
if(flag)
{
System.out.println("出票成功"+Thread.currentThread().getName()+"位置:"+seats);
}else
{
System.out.println("出票失败"+Thread.currentThread().getName()+"位置不够");
}
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。