正文

java编程之银行调度系统2012-05-21 18:23:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/fkjava/53470.html

分享到:

这个程序和前一个又不是相似之处,所以在看张孝祥老师的视频之前自己先试着做了一下,结果一做不要紧,从下午2:00多做到晚上快十点了。速度很慢,主要是设计经验不足,很多东西都得摸索,还有些不知道该怎么选择。但所幸最终还是做出来了。    主要用到三个类:WIndow,Consumer,BankSystem         Window:         主要用于对六个窗口的描述,里面提供了private String type;private String index;private boolean busy  = false;这些成员变量,还有work(),及stop()方法。 广州疯狂JAVA          Consumer:         主要是提供了一个增加元素的方法addConsumer(),同时提供三个集合装三种Consumer.         BankSystem:         主调类。分别new了六个Window对象,和三个Consumer对象。所以相当于开启了9个线程。         BankSystem         import java.util.*;import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class BankDispatch {  ArrayList<Window> windowLiabrary = Window.windowLiabrary;  Iterator<Window> windowIterator ;  Iterator<String> consumerIterator ;  Window window ;  Consumer consumer;         public static void main(String[] args) {         new BankDispatch();  }         public BankDispatch(){         new Consumer("ordinary");         new Consumer("express");         new Consumer("VIP");         new Window("ordinary", "1");         new Window("ordinary", "2");         new Window("ordinary", "3");         new Window("ordinary", "4");         new Window("express", "");         new Window("VIP", "");         while(true){         try {         Thread.sleep(5000);         } catch (InterruptedException e) {         e.printStackTrace();         }         }  }         }Window         package com.renren.interview;import java.util.ArrayList;import java.util.Random;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class Window { public  static ArrayList<Window> windowLiabrary = new ArrayList<Window>(); private String type; private String index;         private boolean busy  = false;         int ordinaryIndex,expressIndex,VIPIndex;         public Consumer consumer;         public int time;         /* (non-Javadoc)  * @see java.lang.Object#hashCode()  */ @Override public int hashCode() {  final int prime = 31;  int result = 1;  result = prime * result + ((index == null) ? 0 : index.hashCode());  result = prime * result + ((type == null) ? 0 : type.hashCode());  return result; } @Override public boolean equals(Object obj) {  if (this == obj)         return true;  if (obj == null)         return false;  if (getClass() != obj.getClass())         return false;  Window other = (Window) obj;  if (index == null) {         if (other.index != null)         return false;  } else if (!index.equals(other.index))         return false;  if (type == null) {         if (other.type != null)         return false;  } else if (!type.equals(other.type))         return false;  return true; }  public String getType(){         return type;         }         public String getIndex(){         return index;         }         public Window(String type,String index) {  this.type = type;  this.index = index;  if(type.equals("ordinary") || type.equals("VIP")){         deal(5);  }  else if(type.equals("express")){         deal(1);  } }         public void deal(int delay){         ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);         timer.scheduleAtFixedRate(new Runnable() {         @Override         public void run() {         //System.out.println(Consumer.ordinaryLibrary.size());         if(consumer!=null){         stop(consumer,time);         }         if(!busy  ){         if(type.equals("ordinary") && Consumer.ordinaryLibrary.contains(new Consumer("ordinary")) ){         work(consumer=Consumer.ordinaryLibrary.remove(0));         }         else if(type.equals("express")){         if(Consumer.expressLibrary.contains(new Consumer("express"))){         work(consumer=Consumer.expressLibrary.remove(0));         }         else         work(consumer = Consumer.ordinaryLibrary.remove(0));         }         else if(type.equals("VIP")){         if(Consumer.VIPLibrary.contains(new Consumer("VIP"))){         work(consumer = Consumer.VIPLibrary.remove(0));         }         else         work(consumer = Consumer.ordinaryLibrary.remove(0));         }         }         }         },    1,         time = (new Random()。nextInt(delay)+1),         TimeUnit.SECONDS);         }         public boolean isBusy(){         return busy;  }         public void work(Consumer consumer){         busy = true;         System.out.println(index+"号普通窗口正在为"+consumer.index+"号普通客户服务");         }         public void stop(Consumer consumer,int time){         busy = false;         System.out.println((index+"号普通窗口为"+consumer.index+"号普通客户服务完成,耗时"+time+"秒"));  //System.out.println(this.type+" window"+this.index+" is not busy");  }}Consumer         se;  } else if (!index.equals(other.index))         return false;  if (type == null) {         if (other.type != null)         return false;  } else if (!type.equals(other.type))         return false;  return true; }  public String getType(){         return type;         }         public String getIndex(){         return index;         }         public Window(String type,String index) {  this.type = type;  this.index = index;  if(type.equals("ordinary") || type.equals("VIP")){         deal(5);  }  else if(type.equals("express")){         deal(1);  } }         public void deal(int delay){         ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);         timer.scheduleAtFixedRate(new Runnable() {         @Override         public void run() {         //System.out.println(Consumer.ordinaryLibrary.size());         if(consumer!=null){         stop(consumer,time);         }         if(!busy  ){         if(type.equals("ordinary") && Consumer.ordinaryLibrary.contains(new Consumer("ordinary")) ){         work(consumer=Consumer.ordinaryLibrary.rem         package com.renren.interview;import java.util.ArrayList;import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class Consumer {  public static int ordinaryIndex,expressIndex,VIPIndex;  private String type;  public static ArrayList<Consumer> ordinaryLibrary = new ArrayList<Consumer>();  public static ArrayList<Consumer> expressLibrary = new ArrayList<Consumer>();  public static ArrayList<Consumer> VIPLibrary = new ArrayList<Consumer>();  public  ArrayList<Consumer> consumerLibrary1 ;  public int index;  public Consumer(String type){         this.type = type;         if(type.equals("ordinary")){         System.out.println((++ordinaryIndex)+"号普通客户等待服务");         index = ordinaryIndex;         addConsumer(1,ordinaryLibrary);         }         else if(type.equals("express")){         System.out.println((++expressIndex)+"号快速客户等待服务");         index = expressIndex;         addConsumer(2,expressLibrary);         }         else if(type.equals("VIP")){         System.out.println((++VIPIndex)+"号VIP客户等待服务");         index = VIPIndex;         addConsumer(6,VIPLibrary);         }         }         public void addConsumer(final int delay, ArrayList<Consumer> consumerLibrary){         this.consumerLibrary1 = consumerLibrary;         ExecutorService pool = Executors.newSingleThreadExecutor();         pool.execute(new Runnable() {         @Override         public void run() {         for(int i=1;i<1000;i++){         try {         Thread.sleep(/*(new Random()。nextInt(delay)+5)*/delay*1000);         consumerLibrary1.add(new Consumer(type));         System.out.println(ordinaryLibrary.size());         } catch (InterruptedException e) {         // TODO Auto-generated catch block         e.printStackTrace();         }         }         }         });         }/* (non-Javadoc) * @see java.lang.Object#hashCode() */@Overridepublic int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((type == null) ? 0 : type.hashCode()); return result;}/* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */@Overridepublic boolean equals(Object obj) { if (this == obj)  return true; if (obj == null)  return false; if (getClass() != obj.getClass())  return false; Consumer other = (Consumer) obj; if (type == null) {  if (other.type != null)         return false; } else if (!type.equals(other.type))  return false; return true;}         }         但最后的执行效果还是稍微不太满意,当然看了张孝祥老师写的代码,还是学的了很多,尤其是其中分析问题的思路。 上一页  [1] [2] 

阅读(704) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册