java 多线程 模拟一台电梯的工作状态

这篇博客通过Java实现了一个单线程模拟电梯的工作状态,利用Stack和链表管理电梯及各楼层的乘客。创建Work类作为线程任务,根据电梯上行和下行的状态进行乘客的进出操作。通过随机生成乘客的目标楼层,模拟了电梯的运行过程。
package 电梯03;
/* 单线程电梯(1台电梯运行,10层)模拟:
 * 	Stack 表示电梯所能装载的人数
 *  下面两个链表表示每层楼对应的人 0未启用
 *  LinkList[] up = new LinkList[11];	want=1 向上去的人所在队列
	LinkList[] down = new LinkList[11];	want=-1 向下去的人所在队列
	Cus 乘客:
		at:所在层数
		to:目的层数
		want:1表示向上去,-1表示向下去
		key:乘客编号
 */
public class Test {
	public static void main(String[] args){
		LinkList[] up = new LinkList[11];
		LinkList[] down = new LinkList[11];
		for(int k=0;k<11;k++){//对每个链表进行初始化
			up[k]=new LinkList();
			down[k]=new LinkList();
		}
		
		Stack st = new Stack();
		
		Work wokk = new Work(st,up,down,0);
		Thread t1 = new Thread(wokk);
		t1.start();
		
		Cus[] cus=new Cus[20];
		for(int i=0;i<20;i++){
			while(true){
				int a=(int)(Math.random()*10+1);
				int t=(int)(Math.random()*10+1);
				if(a<t&&t!=0&&a!=0){
					cus[i] = new Cus(a,t,i,1);
					up[a].add(cus[i]);
					break;
				}else if(a>t&&t!=0&&a!=0){
					cus[i] = new Cus(a,t,i,-1);
					down[a].add(cus[i]);					
					break;
				}
				
			}
			wokk.sleep();
		}
		
	}
}
class Work implements Runnable{
	int work;
	int stay;
	Stack dt;
	boolean asd=true;
	LinkList[] up;
	LinkList[] down;
	public Work(Stack s,LinkList[] u,LinkList[] d, int w){
		dt=s;
		up=u;
		down=d;
		work=w;
		stay=1;
	}
	public void run(){
		while(true){
			if(work==0){//无人状态
				if(asd==false){
					System.out.println("电梯 暂停在"+stay+"楼");				
					sleep();
				}else{
					if(up!=null){
						work=1;
					}else if(down!=null){
						work=-1;
					}else{
						sleep();
					}				
				}
			}else if(work==1){//向上工作状态
				sleep();
				System.out.println("电梯到达"+stay+"楼--[上]");
				up_out(stay);//每到达一层检查是否有人--出
				if(up[stay].head!=null){
					up_in(stay);//每到达一层检查是否有人--上
				}

				if(dt.isEmpty()&&isK(up)&&isK(down)){//当电梯内无人 且没人使用电梯时暂停,继续等待
					asd=false;
					work=0;
					stay--;
				}
				if(dt.isEmpty()&&downyouren(stay)){//将上楼的人运输完之后,检查当前楼层上方是否有人下,若有人下 则去接人,若无人下则电梯下楼
					System.out.println("电梯 暂停在"+stay+"------->楼");		
					down_in(stay);
					down_out(stay);
					//up_in(stay);
					//up_out(stay);
				}
				stay++;
				if(stay>10){
					work=-1;
					stay=10;
				}
			}else if(work==-1){//向下工作状态
				sleep();
				System.out.println("电梯到达"+stay+"楼--[下]");
				down_out(stay);
				if(down[stay].head!=null){
					down_in(stay);				
				}
				if(dt.isEmpty()&&isK(up)&&isK(down)){
					asd=false;
					work=0;
					stay++;
				}
				if(dt.isEmpty()&&upyouren(stay)){
					System.out.println("电梯 暂停在"+stay+"------->楼");		
					up_in(stay);
					up_out(stay);
					//down_in(stay);
					//down_out(stay);
				}
				stay--;
				if(stay<1){
					work=1;
					stay=1;
				}
				
			}
			
		}
	}
	
	public boolean upyouren(int s){		
		for(int i=s;i<=10;i++){
			if(up[i].head!=null){
				return true;
			}
			if(down[i].head!=null){
				return true;
			}
		}
		return false;
	}
	public void down_add(int s){
		for(int i=s;i>=1;i--){
			up_in(i);
			up_out(i);
			down_in(i);
			down_out(i);
		}
	}
	public boolean downyouren(int s){		
		for(int i=s;i>=1;i--){
			if(down[i].head!=null){
				return true;
			}
			if(up[i].head!=null){
				return true;
			}
		}
		return false;
	}
	public boolean isK(LinkList[] arr){
		for(int i=1;i<=10;i++){
			if(arr[i].head!=null){
				return false;
			}
		}
		return true;
	}
	public boolean isKU(int s){
		for(int i=s;i<=10;i++){
			if(down[i].head!=null){
				return true;
			}
			if(up[i].head!=null){
				return true;
			}
		}
		return false;
		
	}
	public boolean isKD(int s){
		for(int i=s;i>=1;i--){
			if(down[i].head!=null){
				return true;
			}
			if(up[i].head!=null){
				return true;
			}
		}
		return false;
		
	}
	public void up_in(int i){
		if(up[i].head!=null){
			Node temp=up[i].head;
			while(temp!=null){
				if(dt.top<=10){
					System.out.println(temp.cus.key+"号-at-"+temp.cus.at+"-to-"+temp.cus.to+"楼---【上楼--进电梯】");					
					dt.push(temp.cus);
					up[i].del(temp.cus);
				}else{
					System.out.println("电梯已上满!");
				}
				temp=temp.n;
			}
		}else{
			
		}
	}
	public void up_out(int i){
		if(dt.top>0){
			Node temp = dt.ll.head;
			while(temp!=null){
				if(temp.cus.to==i){
					System.out.println(temp.cus.key+"号-at-"+temp.cus.at+"-to-"+temp.cus.to+"楼---【已下楼--出电梯】");
					dt.pop(temp.cus);
				}
				temp=temp.n;
			}
		}
	}
	public void down_in(int i){
		if(down[i].head!=null){
			Node temp=down[i].head;
			while(temp!=null){
				if(dt.top<=10){
					System.out.println(temp.cus.key+"号-at-"+temp.cus.at+"-to-"+temp.cus.to+"楼---【下楼--进电梯】");					
					dt.push(temp.cus);
					down[i].del(temp.cus);
				}else{
					System.out.println("电梯已上满!");
				}
				temp=temp.n;
			}
		}
	}
	public void down_out(int i){
		if(dt.top>0){
			Node temp = dt.ll.head;
			while(temp!=null){
				if(temp.cus.to==i){
					System.out.println(temp.cus.key+"号-at-"+temp.cus.at+"-to-"+temp.cus.to+"楼---【已下楼--出电梯】");
					dt.pop(temp.cus);
				}
				temp=temp.n;
			}
		}
	}
	public void sleep(){
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class Cus{
	int at;
	int to;
	int key;
	int want;
	public Cus(int a, int t, int k, int w){
		at=a;
		to=t;
		key=k;
		want=w;
	}
}
class Node{
	Node f;
	Node n;
	Cus cus;
	public Node(Cus c){
		f=n=null;
		cus=c;
	}
}
class Stack{
	int top;
	LinkList ll = new LinkList();
	public Stack(){
		top=0;
	}
	public boolean isEmpty(){
		if(top==0){
			return true;
		}
		return false;
	}
	public void push(Cus c){
		if(top<=10){
			top++;
			ll.add(c);
		}else{
			System.out.println("电梯---已满");
		}
	}
	public void pop(Cus c){
		if(top>0){
			ll.del(c);
			top--;
		}else{
			System.out.println("电梯---无人");			
		}
	}
}
class LinkList{
	Node head;
	Node tail;
	public LinkList(){
		head=tail=null;
	}
	public boolean isEmpty(){
		if(head==null){
			return true;
		}
		return false;
	}
	public void add(Cus c){
		Node node = new Node(c);
		if(head!=null){
			tail.n=node;
			node.f=tail;
			tail=node;
			tail.n=null;
		}else{
			head=tail=node;
			head.f=tail.n=null;
		}
	}
	public void del(Cus c){
		Node node = find(c);
		if(node!=null){
			if(node==head){//.f==null
				if(head.n==null){
					head=tail=null;
				}else{
					head=head.n;
					head.f=null;
				}
			}else if(node==tail){
				if(tail.f==null){
					head=tail=null;
				}else{
					tail=tail.f;
					tail.n=null;					
				}
			}else{
				node.f.n=node.n;
				node.n.f=node.f;
			}
		}
	}
	public Node find(Cus c){
		if(head!=null){
			Node node = head;
			while(node!=null){
				if(node.cus.equals(c)){
					return node;
				}
				node=node.n;
			}
		}
		return null;
	}
	
}
















用面向对象方法和面向对象程序设计语言,实现满足下述要求的一个高层建筑电梯活动 仿真程序。 问题域概述 某国际展览中心共 40 层,设有载客电梯10 部(用E0~E9 标识)。 限定条件 (1) 电梯的运行规则是: E0、E1:可到达每层。 E2、E3:可到达1、25~40 层。 E4、E5:可到达1~25 层。 E6、E7:可到达1、2~40 层中的偶数层。 E8、E9:可到达1~39 层中的奇数层。 (2) 每部电梯的最大乘员量均为K 人(K 值可以根据仿真情况在10~18 人之间确定)。 (3) 仿真开始时,各电梯随机地处于其符合运行规则的任意一层,为空梯。 (4) 仿真开始后,有N 人(0<N<1000)在M 分钟(0<M<10)内随机地到达该国际 展览中心的1 层,开始乘梯活动。 (5) 每位乘客初次所要到达的楼层是随机的,令其在合适的电梯处等待电梯到来。 (6) 每位乘客乘坐合适的电梯到达指定楼层后,随机地停留10-120 秒后,再随机 地去往另一楼层,依此类推,当每人乘坐过L 次(每人的L 值不同,在产生乘客时随机地 在1~10 次之间确定)电梯后,第L+1 次为下至底层并结束乘梯行为。到所有乘客结束乘梯 行为时,本次仿真结束。 (7) 电梯运行速度为S 秒/层(S 值可以根据仿真情况在1~5 之间确定),每人上下时 间为T 秒(T 值可以根据仿真情况在2~10 之间确定)。 (8) 电梯运行的方向由先发出请求者决定,不允许后发出请求者改变电梯的当前运 行方向,除非是未被请求的空梯。 (9) 当某层有乘客按下乘梯电钮时,优先考虑离该层最近的、满足条件(8)、能够 最快到达目标层的电梯。 (10) 不允许电梯超员。 开发结果的行为特征 (1) 产生事件的周期为1 秒,每次可产生0 个或多个事件。 (2) 各随机事件由互不相关的伪随机数发生器决定。 (3) 设计一个易于理解的界面,动态显示各梯的载客与运行情况,动态显示各楼层 的人员停留情况与要求乘梯情况;动态显示从仿真开始到目前的时间。 (4) 显示时用应表示出不同的乘客及其当前所要求去往的楼层。例如,12-32 表示标 识为12 的乘客要求去往32 层。 (5) 统计各梯的运行与空闲时间;统计各人发出乘梯要求后的等待时间;仿真结束 后显示这些时间。 (6) 参数K、N、M、S、T 应从命令行输入。 (7) (选做)考虑有些乘客(随机决定)携带的物品体积较大,需占用1~2 人的电 梯空间(随机决定),且上下梯的时间比其他乘客长一倍的情况,再进行相应的仿真(注意, 不是所有的乘客都携带较大体积的物品)。这时,显示乘客及所去往的楼层时要能够识别出 是否携带了较大体积的物品。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值