Java--多线程问题(影院购票)

实现成功购票(同步块)


/**
 * 多线程实现影院购票
 */
public class dxcYingyuan {
    public static void main(String[] args) {
        Cinemn cinemn=new Cinemn(20,"aaa");
        new Thread(new Customer(cinemn,2),"dd").start();
        new Thread(new Customer(cinemn,1),"cc").start();
    }
}
//顾客类
class Customer implements Runnable{
    Cinemn cinemn;//顾客选择的影院
    int seats;//人数

    public Customer(Cinemn cinemn, int seats) {
        this.cinemn = cinemn;
        this.seats = seats;
    }

    @Override
    public void run() {
        synchronized (cinemn){
            boolean flag=cinemn.bookTickets(seats);
            if(flag){
                System.out.println("出票成功"+Thread.currentThread().getName()+"--位置为"+seats);
            }else {
                System.out.println("出票失败"+Thread.currentThread().getName()+"票数不够");
            }
        }
    }
}
//影院类
class Cinemn{
    int available;//可用的位置
    String name;//影院名称

    public Cinemn(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;
    }
}

实现选择位置功能

import java.util.ArrayList;
import java.util.List;

public class dxcYingyuan01 {
    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);
        //顾客1想要位置
        List<Integer> seats1=new ArrayList<Integer>();
        seats1.add(1);
        seats1.add(2);
        seats1.add(3);
        //顾客2想要位置
        List<Integer> seats2=new ArrayList<Integer>();
        seats2.add(4);
        seats2.add(5);

       Cinemn01 cinemn01=new Cinemn01("坑死人影院",available);
       new Thread(new Customer01(cinemn01,seats1),"顾客1").start();
       new Thread(new Customer01(cinemn01,seats2),"顾客2").start();
    }
}
//顾客类
class Customer01 implements Runnable{
    Cinemn01  cinemn01;//顾客选则的影院名
    List<Integer> seats;//顾客需要的位置

    public Customer01(Cinemn01 cinemn01, List<Integer> seats) {
        this.cinemn01 = cinemn01;
        this.seats = seats;
    }

    @Override
    public void run() {
        synchronized (cinemn01){
            boolean flag=cinemn01.bookTickets(seats);
            if(flag){
                System.out.println("出票成功"+Thread.currentThread().getName()+"--位置为"+seats);
            }else {
                System.out.println("出票失败"+Thread.currentThread().getName()+"--票数不够");
            }
        }
    }
}
//影院类
class Cinemn01{
    String name;//影院名称
    List<Integer> available;//影院可用位置

    public Cinemn01(String name, List<Integer> available) {
        this.name = name;
        this.available = available;
    }
    //购票
    public boolean bookTickets(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;
        }
        available=copy;
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值