java

位置:IT落伍者 >> java >> 浏览文章

java编程之银行调度系统


发布日期:2021年01月28日
 
java编程之银行调度系统
这个程序和前一个又不是相似之处所以在看张孝祥老师的视频之前自己先试着做了一下结果一做不要紧从下午:多做到晚上快十点了速度很慢主要是设计经验不足很多东西都得摸索还有些不知道该怎么选择但所幸最终还是做出来了 主要用到三个类WIndowConsumerBankSystem

Window:

主要用于对六个窗口的描述里面提供了private String type;private String index;private boolean busy = false;这些成员变量还有work()及stop()方法

Consumer:

主要是提供了一个增加元素的方法addConsumer()同时提供三个集合装三种Consumer

BankSystem:

主调类分别new了六个Window对象和三个Consumer对象所以相当于开启了个线程

BankSystem

import javautil*;import javautilRandom;import ncurrentExecutorService;import ncurrentExecutors;public class BankDispatch { ArrayList<Window> windowLiabrary = WindowwindowLiabrary; 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 {

Threadsleep(

} catch (InterruptedException e) {

eprintStackTrace()

}

} }

}Window

package comrenreninterview;import javautilArrayList;import javautilRandom;import ncurrentExecutors;import ncurrentScheduledExecutorService;import ncurrentTimeUnit;public class Window { public static ArrayList<Window> windowLiabrary = new ArrayList<Window>() private String type; private String index;

private boolean busy = false;

int ordinaryIndexexpressIndexVIPIndex;

public Consumer consumer;

public int time;

/* (nonJavadoc) * @see javalangObject#hashCode() */ @Override public int hashCode() { final int prime = ; int result = ; result = prime * result + ((index == null) ? : indexhashCode()) result = prime * result + ((type == null) ? : typehashCode()) return result; } @Override public boolean equals(Object obj) { if (this == obj)

return true; if (obj == null)

return false; if (getClass() != objgetClass())

return false; Window other = (Window) obj; if (index == null) {

if (otherindex != null)

return false; } else if (!indexequals(otherindex))

return false; if (type == null) {

if (othertype != null)

return false; } else if (!typeequals(othertype))

return false; return true; } public String getType(){

return type;

}

public String getIndex(){

return index;

}

public Window(String typeString index) { thistype = type; thisindex = index; if(typeequals(ordinary) || typeequals(VIP)){

deal( } else if(typeequals(express)){

deal( } }

public void deal(int delay){

ScheduledExecutorService timer = ExecutorsnewScheduledThreadPool(

timerscheduleAtFixedRate(new Runnable() {

@Override

public void run() {

//Systemoutprintln(ConsumerordinaryLibrarysize())

if(consumer!=null){

stop(consumertime)

}

if(!busy ){

if(typeequals(ordinary) && Consumerntains(new Consumer(ordinary)) ){

work(consumer=ConsumerordinaryLibraryremove())

}

else if(typeequals(express)){

if(Consumentains(new Consumer(express))){

work(consumer=ConsumerexpressLibraryremove())

}

else

work(consumer = ConsumerordinaryLibraryremove())

}

else if(typeequals(VIP)){

if(Conntains(new Consumer(VIP))){

work(consumer = ConsumerVIPLibraryremove())

}

else

work(consumer = ConsumerordinaryLibraryremove())

}

}

}

}

time = (new Random()nextInt(delay)+

TimeUnitSECONDS)

}

public boolean isBusy(){

return busy; }

public void work(Consumer consumer){

busy = true;

Systemoutprintln(index+号普通窗口正在为+consumerindex+号普通客户服务

}

public void stop(Consumer consumerint time){

busy = false;

Systemoutprintln((index+号普通窗口为+consumerindex+号普通客户服务完成耗时+time+)) //Systemoutprintln(thistype+ window+thisindex+ is not busy }}Consumer

se; } else if (!indexequals(otherindex))

return false; if (type == null) {

if (othertype != null)

return false; } else if (!typeequals(othertype))

return false; return true; } public String getType(){

return type;

}

public String getIndex(){

return index;

}

public Window(String typeString index) { thistype = type; thisindex = index; if(typeequals(ordinary) || typeequals(VIP)){

deal( } else if(typeequals(express)){

deal( } }

public void deal(int delay){

ScheduledExecutorService timer = ExecutorsnewScheduledThreadPool(

timerscheduleAtFixedRate(new Runnable() {

@Override

public void run() {

//Systemoutprintln(ConsumerordinaryLibrarysize())

if(consumer!=null){

stop(consumertime)

}

if(!busy ){

if(typeequals(ordinary) && Consumerntains(new Consumer(ordinary)) ){

work(consumer=ConsumerordinaryLibraryrem

package comrenreninterview;import javautilArrayList;import javautilRandom;import ncurrentExecutorService;import ncurrentExecutors;import ncurrentScheduledExecutorService;import ncurrentTimeUnit;public class Consumer { public static int ordinaryIndexexpressIndexVIPIndex; 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){

thistype = type;

if(typeequals(ordinary)){

Systemoutprintln((++ordinaryIndex)+号普通客户等待服务

index = ordinaryIndex;

addConsumer(ordinaryLibrary)

}

else if(typeequals(express)){

Systemoutprintln((++expressIndex)+号快速客户等待服务

index = expressIndex;

addConsumer(expressLibrary)

}

else if(typeequals(VIP)){

Systemoutprintln((++VIPIndex)+号VIP客户等待服务

index = VIPIndex;

addConsumer(VIPLibrary)

}

}

public void addConsumer(final int delay ArrayList<Consumer> consumerLibrary){

nsumerLibrary = consumerLibrary;

ExecutorService pool = ExecutorsnewSingleThreadExecutor()

poolexecute(new Runnable() {

@Override

public void run() {

for(int i=;i<;i++){

try {

Threadsleep(/*(new Random()nextInt(delay)+)*/delay*

consumerLibraryadd(new Consumer(type))

Systemoutprintln(ordinaryLibrarysize())

} catch (InterruptedException e) {

// TODO Autogenerated catch block

eprintStackTrace()

}

}

}

})

}/* (nonJavadoc) * @see javalangObject#hashCode() */@Overridepublic int hashCode() { final int prime = ; int result = ; result = prime * result + ((type == null) ? : typehashCode()) return result;}/* (nonJavadoc) * @see javalangObject#equals(javalangObject) */@Overridepublic boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != objgetClass()) return false; Consumer other = (Consumer) obj; if (type == null) { if (othertype != null)

return false; } else if (!typeequals(othertype)) return false; return true;}

}

但最后的执行效果还是稍微不太满意当然看了张孝祥老师写的代码还是学的了很多尤其是其中分析问题的思路

               

上一篇:使用JAVA请求需要Basic身份验证的网页

下一篇:如何使用Java网络启动部署软件