148.电影院订票

    科技2022-07-12  138

    能线程安全的以票数订票

    package zy.thread; public class happyCinema { public static void main(String[] args) { final int tickets = 8; int c1_tickets = 5; int c2_tickets = 5; cinema c = new cinema(tickets, "黎明号"); customer c1 = new customer(c, c1_tickets); customer c2 = new customer(c, c2_tickets); new Thread(c1, "customer1").start(); new Thread(c2, "customer2").start(); } } //影院 class cinema{ int avaiable; //可用位置 String name; //名称 public cinema(int avaiable, String name) { super(); this.avaiable = avaiable; this.name = name; } public boolean bookTickets(int seats) { System.out.println("可用位置是:" + avaiable); if(seats > avaiable) return false; avaiable -= seats; return true; } } //顾客 class customer implements Runnable{ cinema c; int seats; @Override public void run() { boolean flag = false; synchronized (c) { flag = c.bookTickets(seats); } if(flag) System.out.println("出票成功" + Thread.currentThread().getName() + " 订购成功:" + seats + "个位置"); else System.out.println("出票失败" + "位置不够"); } public customer(cinema c, int seats) { super(); this.c = c; this.seats = seats; } }

    可以选择座位号的线程安全版

    package zy.thread; import java.util.ArrayList; import java.util.List; /* * 能选位置 */ public class happyCinema02 { public static void main(String[] args) { //座位容器 List<Integer> allSeat = new ArrayList<Integer>(); allSeat.add(1); allSeat.add(5); allSeat.add(7); List<Integer> c1_tickets = new ArrayList<Integer>(); c1_tickets.add(1); List<Integer> c2_tickets = new ArrayList<Integer>(); c2_tickets.add(5); c2_tickets.add(7); c2_tickets.add(8); hapCinema c = new hapCinema(allSeat, "黎明号"); hapCustomer c1 = new hapCustomer(c, c1_tickets); hapCustomer c2 = new hapCustomer(c, c2_tickets); new Thread(c1, "customer1").start(); new Thread(c2, "customer2").start(); } } //影院 class hapCinema{ List<Integer> avaiable; //可用位置 String name; //名称 public hapCinema(List<Integer> avaiable, String name) { super(); this.avaiable = avaiable; this.name = name; } public boolean bookTickets(List<Integer> seats) { System.out.println("可用位置是:" + avaiable); //先复制拷贝一份当前可用位置 List<Integer> copy = new ArrayList<Integer>(); copy.addAll(avaiable); //容器相减 copy.removeAll(seats); if(copy.size() != (avaiable.size() - seats.size())) { for(Integer s : seats) { if(false == copy.contains(s) && false == avaiable.contains(s)) { System.out.println("非法操作," + s + "号座位不存在"); return false; } if(false == avaiable.contains(s)) System.out.println(s+"号座位已经被订购"); } return false; } System.out.println("出票成功" + Thread.currentThread().getName() + " 订购成功:" + seats + "位置"); avaiable = copy; return false; } } //顾客 class hapCustomer implements Runnable{ hapCinema c; List<Integer> seats; @Override public void run() { boolean flag = false; synchronized (c) { flag = c.bookTickets(seats); } if(flag) System.out.println("出票成功" + Thread.currentThread().getName() + " 订购成功:" + seats + "位置"); else System.out.println("出票失败" + "位置不够"); } public hapCustomer(hapCinema c, List<Integer> seats) { super(); this.c = c; this.seats = seats; } }
    Processed: 0.013, SQL: 8