停车场模拟
来源:互联网 发布:centos nginx rtmp 编辑:程序博客网 时间:2024/03/29 19:41
模拟停车场停车的情景,当车位满的时候无法进行停车,当车位空的时候无法把车开走。
停车位类:
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(); }}
0 0
- 停车场模拟
- 停车场模拟
- 停车场模拟
- 停车场模拟
- 停车场模拟
- 模拟停车场管理
- 停车场管理模拟程序
- 停车场管理模拟
- 停车场模拟管理系统
- 项目6 -- 停车场模拟
- 第七周--停车场模拟
- 第七周 停车场模拟
- 第六周--停车场模拟
- 第七周 停车场模拟
- 第七周停车场模拟
- 7周 停车场模拟
- 项目 6 - 模拟停车场
- 模拟停车场管理系统
- 算法导论第六章6.4
- Boost之日期时间处理(date_time库)
- storm官方文档----配置文件说明
- 常用控件
- WebService客户端引用 服务器无法处理请求。 ---> 未将对象引用设置到对象的实例
- 停车场模拟
- Java Web基础学习(二)
- 循环跑号码包的一个小建议
- jQuery中 $(function() 与 (function($) 的区别
- Android网络连接相关笔记
- 记录
- android Merger 代替 FrameLayout:布局优化
- C#设计模式—代理模式
- 《leetCode》:Binary Tree Right Side View