Java多线程实现电影院在线选座

package com.test;

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

/**
 *模拟电影院抢座位
 */
public class Test{
    
    public static void main(String[] args) {
        //可用位置
        List<Integer> available =new ArrayList<Integer>();
        for(int i=1; i<=10; i++) {
            available.add(Integer.valueOf(i)); //默认添加1-10号位置
        }

        //模拟5个顾客,需要的位置
        List<Integer> seats1 =new ArrayList<Integer>();
        seats1.add(1);
        seats1.add(2);
        List<Integer> seats2 =new ArrayList<Integer>();
        seats2.add(3);
        seats2.add(6);
        List<Integer> seats3 =new ArrayList<Integer>();
        seats3.add(4);
        seats3.add(8);
        seats3.add(9);
        List<Integer> seats4=new ArrayList<Integer>();
        seats4.add(2);
        seats4.add(3);
        seats4.add(4);
        
        Cinema cinema = new Cinema(available,"万达影院");
        
        //为每个顾客开辟一个线程,让系统自动调度
        new Thread(new Customer(cinema,seats1),"小华").start();
        new Thread(new Customer(cinema,seats2),"小红").start();
        new Thread(new Customer(cinema,seats3),"小丽").start();
        new Thread(new Customer(cinema,seats4),"小明").start();
    
    
    
    }
    
}
//顾客
class Customer implements Runnable {
    Cinema cinema;
    List<Integer> seats; //顾客选的座位数量
    
    public Customer(Cinema cinema, List<Integer> seats) {
        this.cinema = cinema;
        this.seats = seats;
    }
    
    @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()+"需要"+seats+"-->位置不够");
            }
            System.out.println("当前可用的位置为:"+cinema.available);
            System.out.println("--------------------");
        }
        
    }
}
//电影院
class Cinema {
    List<Integer> available; //可用的座位数量
    String name; //名称
    
    public Cinema(List<Integer> available, String name) {
        this.available = available;
        this.name = name;
    }
    
    //购票
    public boolean bookTickets(List<Integer> seats) {
        
        List<Integer> copy = new ArrayList<>();
        copy.addAll(available);
        
        //移除顾客购买位置
        copy.removeAll(seats);
        //判断
        if(available.size() - copy.size() != seats.size()) {
            return false;
        }
        available = copy;
        return true;
    }
}

运行截图:

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java实现多线程电影院选座可以通过以下步骤实现: 1. 创建一个座位类Seat,包含座位编号、是否已被选中等属性。 2. 创建一个电影院类Cinema,包含座位列表、电影名称等属性,以及选座方法selectSeat()。 3. 在selectSeat()方法中,使用synchronized关键字锁定座位列表,避免多个线程同时选中同一个座位。 4. 在selectSeat()方法中,使用Thread.sleep()模拟选座的时间,以便观察多线程的效果。 5. 创建多个线程,每个线程代表一个用户,调用Cinema的selectSeat()方法选座。 下面是一个简单的示例代码: ```java public class Seat { private int id; private boolean isOccupied; public Seat(int id) { this.id = id; this.isOccupied = false; } public int getId() { return id; } public boolean isOccupied() { return isOccupied; } public void setOccupied(boolean occupied) { isOccupied = occupied; } } public class Cinema { private List<Seat> seats; private String movieName; public Cinema(List<Seat> seats, String movieName) { this.seats = seats; this.movieName = movieName; } public synchronized void selectSeat(int seatId) throws InterruptedException { Seat seat = seats.get(seatId); if (seat.isOccupied()) { System.out.println("Seat " + seatId + " has been occupied."); } else { System.out.println("User " + Thread.currentThread().getName() + " selects seat " + seatId + "."); Thread.sleep(1000); // 模拟选座时间 seat.setOccupied(true); System.out.println("User " + Thread.currentThread().getName() + " successfully selects seat " + seatId + "."); } } } public class Main { public static void main(String[] args) { List<Seat> seats = new ArrayList<>(); for (int i = 0; i < 10; i++) { seats.add(new Seat(i)); } Cinema cinema = new Cinema(seats, "Avengers: Endgame"); for (int i = 0; i < 5; i++) { new Thread(() -> { try { cinema.selectSeat((int) (Math.random() * 10)); } catch (InterruptedException e) { e.printStackTrace(); } }, "User " + i).start(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值