package iostudy.synchro; /** * 多线程模拟影院选位置 * @since JDK 1.8 * @date 2021/6/18 * @author Lucifer */ public class HappyCinema { public static void main(String[] args) { /*实例化资源类对象*/ Cinema c = new Cinema(2, "Happy Game!"); /*实例化代理对象*/ new Thread(new Customer(c,2), "James").start(); new Thread(new Customer(c,1), "Harden").start(); } } /** * 内部资源类,定义资源属性和数量 */ class Cinema{ /*定义资源*/ int available; //可用地位置 String name; //位置名称 /*构造器*/ public Cinema(int available, String name){ this.available = available; this.name = name; } /*购票方法*/ public boolean bookTickets(int seats){ System.out.println("可用位置为:" + available); /*逻辑判断*/ if (seats > available) { return false; } available -= seats; return true; } } /** * 创建顾客类 */ class Customer implements Runnable{ /*传入资源类对象*/ Cinema cinema; int seats; /*创建构造器*/ public Customer(Cinema cinema, int seats){ this.cinema = cinema; this.seats = seats; } /*重写run方法*/ @Override public void run(){ /*对影院对象加锁*/ synchronized (cinema){ boolean flag = cinema.bookTickets(seats); //传入位置判断是true还是false if (true){ System.out.println("出票成功" + Thread.currentThread().getName() + "位置为:" + seats); }else { System.out.println("出票失败!!!"); } } } }
package iostudy.synchro; import java.util.ArrayList; import java.util.List; /** * 多线程模拟影院选位置 * 加入可选位置功能--->利用容器实现 * @since JDk 1.8 * @date 2021/6/18 * @author Lucifer */ public class HappyCinemaNo2 { public static void main(String[] args) { /*实例化容器可用的位置*/ List<Integer> available = new ArrayList<Integer>(); /*添加资源座位*/ available.add(1); available.add(2); available.add(3); 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(3); seats2.add(7); seats2.add(6); /*实例化资源类*/ JunCinema j = new JunCinema(available, "HappyCinema"); /*实例化代理类*/ new Thread(new HappyCustomer(j, seats1), "JamesWilliam").start(); new Thread(new HappyCustomer(j, seats2), "JamesHarden").start(); } } /** * 资源类 */ class HappyCustomer implements Runnable{ /*实例化资源对象*/ JunCinema cinema; List<Integer> seats; /*创建构造器*/ public HappyCustomer(JunCinema cinema, List<Integer> seats){ this.cinema = cinema; this.seats = seats; } /*重写run方法*/ @Override public void run(){ /*设置一个方法开关*/ synchronized (cinema){ boolean flag = cinema.bookTickets(seats); if (flag){ System.out.println("出票成功" + Thread.currentThread().getName() + "位置为:" + seats); }else { System.out.println("出票失败:" + Thread.currentThread().getName()); } } } } /** * 创建影院资源类 */ class JunCinema{ /*创建资源*/ List<Integer> available; String name; /*创建构造器*/ public JunCinema(List<Integer> available, String name){ this.available = available; this.name = name; } /** * 购票选座位方法 */ public boolean bookTickets(List<Integer> seats){ System.out.println("可用位置为:" + available); /*两个容器的比较和操作--->做差比较判断是否可以返回*/ //先拷贝一份原有资源 List<Integer> copy = new ArrayList<Integer>(); /*使用copy方法进行添加*/ copy.addAll(available); //使用copy减掉座位 copy.removeAll(seats); //修改指向 //判断大小 /* 先拷贝位置,然后操作下面的位置 available大小-copy的大小是否等于seats座位的大小 */ if (available.size()-copy.size() != seats.size()){ /*返回失败*/ return false; } //如果成功,直接赋值 available = copy; return true; } }