熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java編程之銀行調度系統

2022-06-13   來源: 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;}
   
    }
   
    但最後的執行效果還是稍微不太滿意當然看了張孝祥老師寫的代碼還是學的了很多尤其是其中分析問題的思路


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26466.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.