Java多线程模拟停车场停车

import java.util.Arrays;
import java.util.Objects;

/**
 * 模拟停车
 */
public class Practice3 {
    public static void main(String[] args) {
        Park park = new Park();
        for (int i = 0; i < 20; i++) {
            new ParkCar(park, new Car("京A" + (int)(Math.random() * 10))).start();
        }
    }
}
class ParkCar extends Thread {
    private final Park park;
    private Car car;
    ParkCar(Park park, Car car) {
        this.park = park;
        this.car = car;
    }
    public void run () {
        synchronized (park) {
            int pos = park.isAvailable();
            if (pos < 0) {
                if (park.isIn(car) > -1) {
                    park.setPosStatu(park.isIn(car), car);
                    car.setStopPos(null);
                    System.out.println(car.toString() + " 取车成功 -> " + park.toString());
                } else {
                    System.out.println("停车场已满!");
                }
            } else {
                if (park.isIn(car) > -1) {
                    park.setPosStatu(park.isIn(car), car);
                    car.setStopPos(null);
                    System.out.println(car.toString() + " 取车成功 -> " + park.toString());
                } else {
                    park.setPosStatu(pos, car);
                    car.setStopPos(pos);
                    System.out.println(car.toString() + " 停车成功 -> " + park.toString());
                }
            }
        }
    }
}
class Park {
    private Car[] status;
    Park() {
        this(3);
    }
    private Park(int capacity) {
        this.status = new Car[capacity];
        Arrays.fill(status, null);
    }
    void setPosStatu(int index, Car car) {
        this.status[index] = this.status[index] != null && this.status[index].equals(car) ? null : car;
    }
    // 是否有空位
    int isAvailable() {
        for (int i = 0; i < this.status.length; i++) {
            if (this.status[i] == null) {
                return i;
            }
        }
        return -1;
    }
    int isIn(Car car) {
        for (int i = 0; i < this.status.length; i++)
            if (car.equals(this.status[i])) {
                return i;
            }
        return -1;
    }
    @Override
    public String toString() {
        return "Park{" +
                "status=" + Arrays.toString(status) +
                '}';
    }
}
class Car {
    private String carId;
    private Integer stopPos;
    Car(String carId) {
        this.carId = carId;
    }
    void setStopPos(Integer stopPos) {
        this.stopPos = stopPos;
    }
    @Override
    public String toString() {
        return "Car{" +
                "carId='" + carId + '\'' +
                ", stopPos=" + stopPos +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Car car = (Car) o;
        return Objects.equals(carId, car.carId);
    }
    @Override
    public int hashCode() {
        return Objects.hash(carId);
    }
}

 

你可能感兴趣的:(后端)