Java编程之银行的调度系统
这个程序和前一个又不是相似之处,所以在看张孝祥老师的视频之前自己先试着做了一下,结果一做不要紧,从下午2:00多做到晚上快十点了。速度很慢,主要是设计经验不足,很多东西都得摸索,还有些不知道该怎么选择。但所幸最终还是做出来了。 主要用到三个类:WIndow,Consumer,BankSystem
Window:
主要用于对六个窗口的描述,里面提供了private String type;private String index;private boolean busy = false;这些成员变量,还有work(),及stop()方法。
Consumer:
主要是提供了一个增加元素的方法addConsumer(),同时提供三个集合装三种Consumer.
BankSystem:
主调类。分别new了六个Window对象,和三个Consumer对象。所以相当于开启了9个线程。
BankSystem
import il.*;import ndom;import ecutorService;import ecutors;public class BankDispatch { ArrayList windowLiabrary = ndowLiabrary; Iterator windowIterator ; Iterator 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 {
eep(5000);
} catch (InterruptedException e) {
intStackTrace();
}
} }
}Window
package erview;import rayList;import ndom;import ecutors;import heduledExecutorService;import meUnit;public class Window { public static ArrayList windowLiabrary = new ArrayList(); private String type; private String index;
private boolean busy = false;
int ordinaryIndex,expressIndex,VIPIndex;
public Consumer consumer;
public int time;
/* (non-Javadoc) * @see ject#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((index == null) ? 0 : shCode()); result = prime * result + ((type == null) ? 0 : shCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj)
return true; if (obj == null)
return false; if (getClass() != tClass())
return false; Window other = (Window) obj; if (index == null) {
if (dex != null)
return false; } else if (!uals(dex))
return false; if (type == null) {
if (pe != null)
return false; } else if (!uals(pe))
return false; return true; } public String getType(){
return type;
}
public String getIndex(){
return index;
}
public Window(String type,String index) { pe = type; dex = index; if(uals("ordinary") || uals("VIP")){
deal(5); } else if(uals("express")){
deal(1); } }
public void deal(int delay){
ScheduledExecutorService timer = wScheduledThreadPool(1);
heduleAtFixedRate(new Runnable() {
@Override
public void run() {
//intln(ze());
if(consumer!=null){
stop(consumer,time);
}
if(!busy ){
if(uals("ordinary") && ntains(new Consumer("ordinary")) ){
work(consumer=move(0));
}
else if(uals("express")){
if(ntains(new Consumer("express"))){
work(consumer=move(0));
}
else
work(consumer = move(0));
}
else if(uals("VIP")){
if(ntains(new Consumer("VIP"))){
work(consumer = move(0));
}
else
work(consumer = move(0));
}
}
}
},
1,
time = (new Random()。nextInt(delay)+1),
CONDS);
}
public boolean isBusy(){
return busy; }
public void work(Consumer consumer){
busy = true;
intln(index+"号普通窗口正在为"+dex+"号普通客户服务");
}
public void stop(Consumer consumer,int time){
busy = false;
intln((index+"号普通窗口为"+dex+"号普通客户服务完成,耗时"+time+"秒")); //intln(pe+" window"+dex+" is not busy"); }}Consumer
se; } else if (!uals(dex))
return false; if (type == null) {
if (pe != null)
return false; } else if (!uals(pe))
return false; return true; } public String getType(){
return type;
}
public String getIndex(){
return index;
}
public Window(String type,String index) { pe = type; dex = index; if(uals("ordinary") || uals("VIP")){
deal(5); } else if(uals("express")){
deal(1); } }
public void deal(int delay){
ScheduledExecutorService timer = wScheduledThreadPool(1);
heduleAtFixedRate(new Runnable() {
@Override
public void run() {
//intln(ze());
if(consumer!=null){
stop(consumer,time);
}
if(!busy ){
if(uals("ordinary") && ntains(new Consumer("ordinary")) ){
work(consumer=m
package erview;import rayList;import ndom;import ecutorService;import ecutors;import heduledExecutorService;import meUnit;public class Consumer { public static int ordinaryIndex,expressIndex,VIPIndex; private String type; public static ArrayList ordinaryLibrary = new ArrayList(); public static ArrayList expressLibrary = new ArrayList(); public static ArrayList VIPLibrary = new ArrayList(); public ArrayList consumerLibrary1 ; public int index; public Consumer(String type){
pe = type;
if(uals("ordinary")){
intln((++ordinaryIndex)+"号普通客户等待服务");
index = ordinaryIndex;
addConsumer(1,ordinaryLibrary);
}
else if(uals("express")){
intln((++expressIndex)+"号快速客户等待服务");
index = expressIndex;
addConsumer(2,expressLibrary);
}
else if(uals("VIP")){
intln((++VIPIndex)+"号VIP客户等待服务");
index = VIPIndex;
addConsumer(6,VIPLibrary);
}
}
public void addConsumer(final int delay, ArrayList consumerLibrary){
nsumerLibrary1 = consumerLibrary;
ExecutorService pool = wSingleThreadExecutor();
ecute(new Runnable() {
@Override
public void run() {
for(int i=1;i<1000;i++){
try {
eep(/*(new Random()。nextInt(delay)+5)*/delay*1000);
d(new Consumer(type));
intln(ze());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
intStackTrace();
}
}
}
});
}/* (non-Javadoc) * @see ject#hashCode() */@Overridepublic int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((type == null) ? 0 : shCode()); return result;}/* (non-Javadoc) * @see ject#equals(ject) */@Overridepublic boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != tClass()) return false; Consumer other = (Consumer) obj; if (type == null) { if (pe != null)
return false; } else if (!uals(pe)) return false; return true;}
}
但最后的执行效果还是稍微不太满意,当然看了张孝祥老师写的代码,还是学的了很多,尤其是其中分析问题的思路。