模拟停车场停车的情景,当车位满的时候无法进行停车,当车位空的时候无法把车开走。
停车位类:
public class Space {
private int id;
private boolean state;
public Space(int id){
this.id = id;
state = false;
}
public void in(){
state = true;
}
public void out(){
state = false;
}
public boolean getState(){
return state;
}
public int getId(){
return id;
}
public String toString(){
if(state){
return id + ": This space is full.";
}else{
return id + ": This space is empty.";
}
}
}
停车场类:
import java.util.*;
public class Park {
private ArrayList<Space> all = new ArrayList<>();
private ArrayList<Space> emptySpace = new ArrayList<>();
private ArrayList<Space> fullSpace = new ArrayList<>();
private int capacity;
private int fullNum;
public Park(int num){
capacity = num;
Random r = new Random();
for(int i = 1; i < capacity + 1; i++){
if(r.nextBoolean()){
Space temp = new Space(i);
temp.in();
fullSpace.add(temp);
all.add(temp);
}else{
Space temp = new Space(i);
temp.out();
emptySpace.add(temp);
all.add(temp);
}
}
fullNum = fullSpace.size();
//emptySpace = (ArrayList) all.clone();
}
//定义停车方法
public synchronized void in(){
if(fullNum < capacity){
int index = (int) (Math.random() * emptySpace.size());
System.out.println(Thread.currentThread().getName() + "在" + emptySpace.get(index).getId() + "车位上停车了");
emptySpace.get(index).in();
fullSpace.add(emptySpace.remove(index));
fullNum++;
notifyAll();
}else{
try{
System.out.println("车位已满");
wait();
}catch (InterruptedException ex){
ex.printStackTrace();
}
}
}
//定义车开走方法
public synchronized void out(){
if(fullNum > 0){
int index = (int) (Math.random() * fullSpace.size());
System.out.println(Thread.currentThread().getName() + "把" + fullSpace.get(index).getId() + "车位上的车开走了");
//System.out.println(fullSpace.get(index).getId() + "车位上的车开走了");
fullSpace.get(index).out();
emptySpace.add(fullSpace.remove(index));
fullNum--;
notifyAll();
}else{
try{
System.out.println("车位已空");
wait();
}catch (InterruptedException ex){
ex.printStackTrace();
}
}
}
public void show(){
System.out.println();
for(int i = 0; i < all.size(); i++){
System.out.println(all.get(i));
}
}
}
停车类,从Thread继续
public class ParkIn extends Thread{
private Park park;
private int num;
public ParkIn(String name, Park park, int num){
super(name);
this.park = park;
this.num = num;
}
public void run(){
for(int i = 0; i < num; i++){
park.in();
}
}
}
车开走类
public class ParkOut extends Thread{
private Park park;
private int num;
public ParkOut(String name, Park park, int num){
super(name);
this.park = park;
this.num = num;
}
public void run(){
for(int i = 0; i < num; i++){
park.out();
}
}
}
测试类,总共有3个停车类,3个开走类。
import java.util.*;
public class ParkTest {
public static void main(String[] args) {
int num = 20;
Random random = new Random();
Park p = new Park(random.nextInt(num));
p.show();
ParkIn pi1 = new ParkIn("IN:A", p, random.nextInt(num));
ParkIn pi2 = new ParkIn("IN:B", p, random.nextInt(num));
ParkIn pi3 = new ParkIn("IN:C", p, random.nextInt(num));
ParkOut po1 = new ParkOut("OUT A", p, random.nextInt(num));
ParkOut po2 = new ParkOut("OUT B", p, random.nextInt(num));
ParkOut po3 = new ParkOut("OUT C", p, random.nextInt(num));
pi1.start();
pi2.start();
pi3.start();
po1.start();
po2.start();
po3.start();
//p.show();
}
}