知识大全 java编程之银行调度系统
Posted 客户
篇首语:知识的根是苦的,它的果实是甜的。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 java编程之银行调度系统相关的知识,希望对你有一定的参考价值。
这个程序和前一个又不是相似之处 所以在看张孝祥老师的视频之前自己先试着做了一下 结果一做不要紧 从下午 : 多做到晚上快十点了 速度很慢 主要是设计经验不足 很多东西都得摸索 还有些不知道该怎么选择 但所幸最终还是做出来了 主要用到三个类 WIndow Consumer BankSystem Window: 主要用于对六个窗口的描述 里面提供了private String type;private String index;private boolean busy = false;这些成员变量 还有work() 及stop()方法 Consumer: 主要是提供了一个增加元素的方法addConsumer() 同时提供三个集合装三种Consumer BankSystem: 主调类 分别new了六个Window对象 和三个Consumer对象 所以相当于开启了 个线程 BankSystem import java util *;import java util Random;import ncurrent ExecutorService;import ncurrent 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 ) new Window( ordinary ) new Window( ordinary ) new Window( ordinary ) new Window( express ) new Window( VIP ) while(true) try Thread sleep( ) catch (InterruptedException e) e printStackTrace() Window package renren interview;import java util ArrayList;import java util Random;import ncurrent Executors;import ncurrent ScheduledExecutorService;import ncurrent 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 = ; int result = ; result = prime * result + ((index == null) ? : index hashCode()) result = prime * result + ((type == null) ? : 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( ) else if(type equals( express )) deal( ) public void deal(int delay) ScheduledExecutorService timer = Executors newScheduledThreadPool( ) 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 ) && Consumerntains(new Consumer( ordinary )) ) work(consumer=Consumer ordinaryLibrary remove( )) else if(type equals( express )) if(Consumentains(new Consumer( express ))) work(consumer=Consumer expressLibrary remove( )) else work(consumer = Consumer ordinaryLibrary remove( )) else if(type equals( VIP )) if(Conntains(new Consumer( VIP ))) work(consumer = Consumer VIPLibrary remove( )) else work(consumer = Consumer ordinaryLibrary remove( )) time = (new Random() nextInt(delay)+ ) 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( ) else if(type equals( express )) deal( ) public void deal(int delay) ScheduledExecutorService timer = Executors newScheduledThreadPool( ) 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 ) && Consumerntains(new Consumer( ordinary )) ) work(consumer=Consumer ordinaryLibrary rem package renren interview;import java util ArrayList;import java util Random;import ncurrent ExecutorService;import ncurrent Executors;import ncurrent ScheduledExecutorService;import ncurrent 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> consumerLibrary ; public int index; public Consumer(String type) this type = type; if(type equals( ordinary )) System out println((++ordinaryIndex)+ 号普通客户等待服务 ) index = ordinaryIndex; addConsumer( ordinaryLibrary) else if(type equals( express )) System out println((++expressIndex)+ 号快速客户等待服务 ) index = expressIndex; addConsumer( expressLibrary) else if(type equals( VIP )) System out println((++VIPIndex)+ 号VIP客户等待服务 ) index = VIPIndex; addConsumer( VIPLibrary) public void addConsumer(final int delay ArrayList<Consumer> consumerLibrary) nsumerLibrary = consumerLibrary; ExecutorService pool = Executors newSingleThreadExecutor() pool execute(new Runnable() @Override public void run() for(int i= ;i< ;i++) try Thread sleep(/*(new Random() nextInt(delay)+ )*/delay* ) consumerLibrary 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 = ; int result = ; result = prime * result + ((type == null) ? : 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; 但最后的执行效果还是稍微不太满意 当然看了张孝祥老师写的代码 还是学的了很多 尤其是其中分析问题的思路 cha138/Article/program/Java/hx/201311/26466相关参考
Java编程思想之静态技术 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 静态技术包括静态数据静
Java并发编程实践之ThreadLocal变量 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
Java高级编程之displayTag学习摘要 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! t
Java多线程编程基础之线程对象 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &nbs
JAVA编程解析之classpath的深入理解 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 现
实战Java多线程编程精要之高级支持 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 线程组 线
实战体会Java多线程编程精要之基础 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n
Java编程思想之中间层检测类 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 首先请看如下的代码
Java套接字实现网络编程之基础篇 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!用Java开发网络
JavaApplet编程之实现显示图像 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! JavaA