银行业务调度系统的实现

                今天,突然看到了想去码银行业务调度系统

按照字母的排序发我的代码吧!!

 C字母

package bank;

public class Consumer {

	public  Integer id;
	public String type;
	
	
	
	public Consumer(Integer id,String type) {
		
		this.id = id;
		this.type = type;
	}
	
	public void execute(){
		
		try {
			if(type.equals("normal")) {
				Thread.sleep(1000);
				return;
				
			}
			if(type.equals("quick")) {
				Thread.sleep(300);
				return;
			}
			if(type.equals("vip")) {
				Thread.sleep(1000);
				return;
			}
			
		}catch(Exception E) {
			E.printStackTrace();
		}
		
		
	}
	
	
	
	
	
}

 D字母

    package bank;  
      
    import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;  
      

    public class Dispatcher {  
      
        /** 
         * @param threadPool 
         * @param Windows 
         * @param vips 
         * @param normals 
         * @param quicks 
         */  
        public void doQuick(ExecutorService threadPool, WindowList Windows,   
                ArrayBlockingQueue<Consumer> vips,  
                ArrayBlockingQueue<Consumer> normals, ArrayBlockingQueue<Consumer> quicks) {  
            try {  
                while (true) {  
                    // do vip  
                    final Window window = Windows.getIdelQuickWindow();  
                    if (window != null) {  
      
                        int flag = 0;  
      
                        final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);  
                        if (id_quick != null)  
                            // quick里面有人  
                            run(window, id_quick, threadPool);  
                        flag = 1;  
      
                        if (flag == 0) {  
                            final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);  
                            if (id_vip != null)  
                                // quick里面没人 vip队伍里有人排队  
                                run(window, id_vip, threadPool);  
                            flag = 1;  
      
                        }  
                        if (flag == 0) {  
                            final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);  
                            if (id_normal != null) {  
                                // quick也没有人 vip队伍里没人排队 normal 里面有人  
                                run(window, id_normal, threadPool);  
                            }  
      
                        }  
                    } else {  
                        // vip窗口正在忙  
                        Thread.sleep(1000);  
                    }  
      
                }  
      
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
        }  
      
        /** 
         * @param id_quick 
         * @param threadPool 
         */  
        private static void run(final Window window, final Consumer id_quick, ExecutorService threadPool) {  
            threadPool.execute(new Runnable() {  
                public void run() {  
                    window.execute(id_quick);  
                }  
            });  
      
        }  
      
        /** 
         * @param threadPool 
         * @param list 
         * @param vips 
         */  
        public void doNomal(ExecutorService threadPool, WindowList list, ArrayBlockingQueue<Consumer> vips,  
                ArrayBlockingQueue<Consumer> normals, ArrayBlockingQueue<Consumer> quicks) {  
      
            try {  
                while (true) {  
                    // do vip  
                    final Window window = list.getIdelNormalWindow();  
                    if (window != null) {  
      
                        int flag = 0;  
      
                        final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);  
                        if (id_normal != null)  
                            // normal 里面有人  
                            run(window, id_normal, threadPool);  
                        flag = 1;  
      
                        if (flag == 0) {  
                            final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);  
                            if (id_quick != null)  
                                // normal里面没人 quick里面有人  
                                run(window, id_quick, threadPool);  
                            flag = 1;  
                        }  
      
                        if (flag == 0) {  
                            final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);  
                            if (id_vip != null)  
                                // normal里面没人 normal里面没人 vip队伍里有人排队  
                                run(window, id_vip, threadPool);  
                            flag = 1;  
      
                        }  
                    } else {  
                        // normal窗口正在忙  
                        Thread.sleep(1000);  
                    }  
      
                }  
      
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
        }  
      
        /** 
         * @param vips 
         * @param list 
         * @param threadPool 
         * @param quicks 
         * @param normals 
         *  
         */  
        public void doVIP(ExecutorService threadPool, WindowList list, ArrayBlockingQueue<Consumer> vips,  
                ArrayBlockingQueue<Consumer> normals, ArrayBlockingQueue<Consumer> quicks) {  
      
            try {  
                while (true) {  
                    // do vip  
                    final Window window = list.getIdelVIPWindow();  
                    if (window != null) {  
                        // vip窗口空闲  
                        // System.out.println("vips is null? "+vips==null);  
                        final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);  
      
                        int flag = 0;  
                        if (id_vip != null) {  
                            // vip队伍里有人排队  
                            run(window, id_vip, threadPool);  
                            flag = 1;  
                        }  
      
                        if (flag == 0) {  
                            final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);  
                            if (id_quick != null)  
                                // vip队伍里没人排队 quick里面有人  
                                run(window, id_quick, threadPool);  
                            flag = 1;  
      
                        }  
      
                        if (flag == 0) {  
                            final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);  
                            if (id_normal != null)  
                                // vip队伍里没人排队 quick也里面有人 normal 里面有人  
                                run(window, id_normal, threadPool);  
      
                        }  
      
                    } else {  
                        // vip窗口正在忙  
                        Thread.sleep(1000);  
                    }  
      
                }  
      
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
        }  
      
    }  

M字母

package bank;  
  
import java.util.concurrent.ArrayBlockingQueue;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
import java.util.concurrent.ThreadPoolExecutor;  
  

public class Main {  
  
    public static void main(String[] args) {  
  
        final ExecutorService threadPool = Executors.newCachedThreadPool();  
  
        // 每个队伍 最多有10人  
        final ArrayBlockingQueue<Consumer> normals = new ArrayBlockingQueue<>(10);  
        final ArrayBlockingQueue<Consumer> vips = new ArrayBlockingQueue<>(10);  
        final ArrayBlockingQueue<Consumer> quicks = new ArrayBlockingQueue<>(10);  
  
        final WindowList Windows = new WindowList();  
  
        final Dispatcher dispatcher=new Dispatcher();  
        final Producer producer = new Producer();  
        producer.setNormals(normals);  
        producer.setQuicks(quicks);  
        producer.setVips(vips);  
  
        threadPool.execute(new Runnable() {  
            public void run() {  
                producer.produce();  
            }  
        });  
  
        threadPool.execute(new Runnable() {  
            public void run() {  
                dispatcher.doVIP(threadPool, Windows, vips, normals, quicks);  
            }  
        });  
  
        threadPool.execute(new Runnable() {  
            public void run() {  
                dispatcher.doQuick(threadPool, Windows, vips, normals, quicks);  
            }  
        });  
  
        threadPool.execute(new Runnable() {  
            public void run() {  
                dispatcher.doNomal(threadPool, Windows, vips, normals, quicks);  
            }  
        });  
  
        threadPool.execute(new Runnable() {  
            public void run() {  
  
                try {  
                    while (true) {  
                        // http://blog.csdn.net/yingzishizhe/article/details/8769907  
                        int threadCount = ((ThreadPoolExecutor) threadPool).getActiveCount();  
                        System.out.println("现在活跃的线程数量为: " + threadCount);  
                        System.out.println("现在排队的人数为:" + (vips.size() + normals.size() + quicks.size()));  
                        Thread.sleep(3000);  
                    }  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
  
            }  
        });  
  
    }  
}  

P字母

package bank;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;

public class Producer {  
  
  
    //省略get/set方法  
    public ArrayBlockingQueue<Consumer> normals;  
    public ArrayBlockingQueue<Consumer> vips;  
    public ArrayBlockingQueue<Consumer> quicks;  
      
          
    public void produce(){  
        Random random=new Random();  
        Random time=new Random();  
        int i=1;  
        try {  
              
            while (true) {  
                //产生1-10  
                int type=random.nextInt(10)+1;   
                if (type==1) {  
                    System.out.println("产生第"+i+"个客户,他是vip用户");  
                    vips.put(new Consumer(i++, "vip"));  
                }else if (type<5) {  
                    System.out.println("产生第"+i+"个客户,他是快速用户");  
                    quicks.put(new Consumer(i++, "快速"));  
                }else {  
                    System.out.println("产生第"+i+"个客户,他是普通用户");  
                    normals.put(new Consumer(i++, "普通"));  
                }  
                  
                Thread.sleep(time.nextInt(1000));  
            }  
              
              
        } catch (Exception e) {  
            // TODO: handle exception  
        }  
          
    }


	public ArrayBlockingQueue<Consumer> getNormals() {
		return normals;
	}


	public void setNormals(ArrayBlockingQueue<Consumer> normals) {
		this.normals = normals;
	}


	public ArrayBlockingQueue<Consumer> getVips() {
		return vips;
	}


	public void setVips(ArrayBlockingQueue<Consumer> vips) {
		this.vips = vips;
	}


	public ArrayBlockingQueue<Consumer> getQuicks() {
		return quicks;
	}


	public void setQuicks(ArrayBlockingQueue<Consumer> quicks) {
		this.quicks = quicks;
	}  
    
    
    
    
    
    
}  

T字母

/*package bank;

import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class Ts0 {

       public static void main(String[] args) {
             // 产生4个普通窗口
             for (int i = 1; i < 5; i++) {
                  ServiceWindow window = new ServiceWindow();
                  window.setNumber(i);
                  window.start();
            }

             // 产生1个快速窗口
            ServiceWindow expressWindow = new ServiceWindow();
            expressWindow.setType(CustomerType. EXPRESS);
            expressWindow.start();

             // 产生1个VIP窗口
            ServiceWindow vipWindow = new ServiceWindow();
            vipWindow.setType(CustomerType. VIP);
            vipWindow.start();
             // 普通客户拿号
            Executors. newScheduledThreadPool(1).scheduleAtFixedRate( new Runnable() {
                   public void run() {
                        Integer serviceNumber = NumMachine.getNumMachine()
                                    .getCommonManager().addNum();
                        System. out.println("第" + serviceNumber + "号普通客户正在等待服务!" );
                  }
            }, 0, Constants. COMMON_CUSTOMER_INTERVAL_TIME, TimeUnit.SECONDS );
             // 快速客户拿号
            Executors. newScheduledThreadPool(1).scheduleAtFixedRate( new Runnable() {
                   public void run() {
                        Integer serviceNumber = NumMachine.getNumMachine()
                                    .getExpressManager().addNum();
                        System. out.println("第" + serviceNumber + "号快速客户正在等待服务!" );
                  }
            }, 0, Constants. COMMON_CUSTOMER_INTERVAL_TIME * 2, TimeUnit.SECONDS );

             // VIP客户拿号
            Executors. newScheduledThreadPool(1).scheduleAtFixedRate( new Runnable() {
                   public void run() {
                        Integer serviceNumber = NumMachine.getNumMachine()
                                    .getVipManager().addNum();
                        System. out.println("第" + serviceNumber + "号VIP客户正在等待服务!" );
                  }
            }, 0, Constants. COMMON_CUSTOMER_INTERVAL_TIME * 6, TimeUnit.SECONDS );
      }
}


 * 服务窗口
 
class ServiceWindow {
       private static Logger logger = Logger.getLogger("cn.itcast.bankqueue");
       private CustomerType type = CustomerType.COMMON;
       private int number = 1;

       public CustomerType getType() {
             return type ;
      }

       public void setType(CustomerType type) {
             this.type = type;
      }

       public void setNumber(int number) {
             this.number = number;
      }

       public void start() {
            Executors. newSingleThreadExecutor().execute(new Runnable() {
                   public void run() {
                         // 下面这种写法的运行效率低,最好是把while放在case下面
                         while (true ) {
                               switch (type ) {
                               case COMMON:
                                    commonService();
                                     break;
                               case EXPRESS :
                                    expressService();
                                     break;
                               case VIP :
                                    vipService();
                                     break;
                              }
                        }
                  }
            });
      }

       private void commonService() {
            String windowName = "第" + number + "号" + type + "窗口" ;
            System. out.println(windowName + "开始获取普通任务!" );
            Integer serviceNumber = NumMachine.getNumMachine().getCommonManager()
                        .fetchNum();
             if (serviceNumber != null) {
                  System. out.println(windowName + "开始为第" + serviceNumber + "号普通客户服务" );
                   int maxRandom = Constants.MAX_SERVICE_TIME
                              - Constants. MIN_SERVICE_TIME;
                   int serviceTime = new Random().nextInt(maxRandom) + 1
                              + Constants. MIN_SERVICE_TIME;

                   try {
                        Thread. sleep(serviceTime);
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
                  System. out.println(windowName + "完成为第" + serviceNumber
                              + "号普通客户服务,总共耗时" + serviceTime / 1000 + "秒" );
            } else {
                  System. out.println(windowName + "没有取到普通任务,正在空闲一秒" );
                   try {
                        Thread. sleep(1000);
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
            }
      }

       private void expressService() {
            Integer serviceNumber = NumMachine.getNumMachine().getExpressManager()
                        .fetchNum();
            String windowName = "第" + number + "号" + type + "窗口" ;
            System. out.println(windowName + "开始获取快速任务!" );
             if (serviceNumber != null) {
                  System. out.println(windowName + "开始为第" + serviceNumber + "号快速客户服务" );
                   int serviceTime = Constants.MIN_SERVICE_TIME;
                   try {
                        Thread. sleep(serviceTime);
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
                  System. out.println(windowName + "完成为第" + serviceNumber
                              + "号快速客户服务,总共耗时" + serviceTime / 1000 + "秒" );
            } else {
                  System. out.println(windowName + "没有取到快速任务!" );
                  commonService();
            }
      }

       private void vipService() {

            Integer serviceNumber = NumMachine.getNumMachine().getVipManager()
                        .fetchNum();
            String windowName = "第" + number + "号" + type + "窗口" ;
            System. out.println(windowName + "开始获取VIP任务!" );
             if (serviceNumber != null) {
                  System. out
                              .println(windowName + "开始为第" + serviceNumber + "号VIP客户服务" );
                   int maxRandom = Constants.MAX_SERVICE_TIME
                              - Constants. MIN_SERVICE_TIME;
                   int serviceTime = new Random().nextInt(maxRandom) + 1
                              + Constants. MIN_SERVICE_TIME;
                   try {
                        Thread. sleep(serviceTime);
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
                  System. out.println(windowName + "完成为第" + serviceNumber
                              + "号VIP客户服务,总共耗时" + serviceTime / 1000 + "秒" );
            } else {
                  System. out.println(windowName + "没有取到VIP任务!" );
                  commonService();
            }
      }
}


 * 客户类型
 
enum CustomerType {
       COMMON, EXPRESS , VIP;
       public String toString() {
            String name = null;
             switch (this ) {
             case COMMON:
                  name = "普通";
                   break;
             case EXPRESS :
                  name = "快速";
                   break;
             case VIP :
                  name = name();
                   break;
            }
             return name;
      }
}


 * 排号器
 
class NumManager {
       private int lastNum = 0;
       private List queueNums = new ArrayList();

       // 添加线程
       public synchronized Integer addNum() {
             queueNums.add(++lastNum);
             return lastNum ;
      }

       // 取出线程
       public synchronized Integer fetchNum() {
             if (lastNum > 0) {
                   queueNums.remove(0);
                   return lastNum ;
            }
             return null ;
      }
}


 * 取号器(单例)
 
class NumMachine {
       // 单例
       private NumMachine() {
      }

       private static NumMachine numMachine = new NumMachine();

       public static NumMachine getNumMachine() {
             return numMachine ;
      }

       // 分三个排号器
       private NumManager commonManager = new NumManager();
       private NumManager expressManager = new NumManager();
       private NumManager vipManager = new NumManager();

       public NumManager getCommonManager() {
             return commonManager ;
      }

       public NumManager getExpressManager() {
             return expressManager ;
      }

       public NumManager getVipManager() {
             return vipManager ;
      }
}


 * 客户
 
class Constants {
       public static int MAX_SERVICE_TIME = 10000; // 10秒!
       public static int MIN_SERVICE_TIME = 1000; // 1秒!

       
       * 每个普通窗口服务一个客户的平均时间为5秒,一共有4个这样的窗口,也就是说银行的所有普通窗口合起来
       * 平均1.25秒内可以服务完一个普通客户,再加上快速窗口和VIP窗口也可以服务普通客户,所以, 1秒钟产生一个普通客户比较合理,
       
       public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;
}

*/

W字母:

    package bank;  
      

    public abstract class Window {  
      
      
        public int id;  
        public String type;  
        public Boolean isBusy;  
      
      
        public void execute(Consumer client) {  
            System.out.println(id + "号" + type + "业务员 开始办理" + client.id + "号" + client.type + "顾客的业务");  
            isBusy = true;  
            client.execute();  
            isBusy = false;  
            System.out.println(id + "号normal业务员 办理完了" + client.id + "号" + client.type + "顾客的业务");  
        }  
    }  
      
      
    class WindowForVIP extends Window {  
      
      
        public WindowForVIP(int i) {  
            id = i;  
            type = "vip";  
            isBusy = false;  
        }  
    }  
      
      
    class WindowForNormal extends Window {  
      
      
        public WindowForNormal(int i) {  
            id = i;  
            type = "normal";  
            isBusy = false;  
        }  
      
      
    }  
      
      
    class WindowForQuick extends Window {  
      
      
        public WindowForQuick(int i) {  
            id = i;  
            type = "quick";  
            isBusy = false;  
        }  
      
      
    }  

W字母

package bank;

import java.util.ArrayList;
import java.util.List;



public class WindowList {  
    public List<WindowForNormal> normallist=new ArrayList<>();  
    public List<WindowForVIP> viplist=new ArrayList<>();  
    public List<WindowForQuick> quicklist=new ArrayList<>();  
      
    public WindowList(){  
        for(int i=1;i<5;i++)  
            normallist.add(new WindowForNormal(i));  
          
        quicklist.add(new WindowForQuick(5));  
        viplist.add(new WindowForVIP(6));  
    }  
      
    public Window getIdelVIPWindow(){  
        for(Window s:viplist){  
            if(!s.isBusy){  
                return s;  
            }  
        }  
        return null;  
    }  
      
    public Window getIdelNormalWindow(){  
        for(Window s:normallist){  
            if(!s.isBusy){  
                return s;  
            }  
        }  
        return null;  
    }  
      
    public Window getIdelQuickWindow(){  
        for(Window s:quicklist){  
            if(!s.isBusy){  
                return s;  
            }  
        }  
        return null;  
    }  
      
}  

然后开启mian方法就能模拟银行业务窗口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用Java编写银行业务调度系统,附主类: package com.isoftstons.interview.bank; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Mainlass { public Mainlass() { } public static void main(String[] args) { ServiceWindow vipWindow; for(int i = 1; i < 5; ++i) { vipWindow = new ServiceWindow(); vipWindow.setWindowId(i); vipWindow.start(); } ServiceWindow expressWindow = new ServiceWindow(); expressWindow.setType(CustomerType.EXPRESS); expressWindow.start(); vipWindow = new ServiceWindow(); vipWindow.setType(CustomerType.VIP); vipWindow.start(); Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { public void run() { Integer number = NumberMachine.getInstance().getCommonManager().generateNewManager(); System.out.println(number + "号普通客户等待服务"); } }, 0L, (long)Constants.COMMON_CUSTOMER_INTERVAL_TIME, TimeUnit.SECONDS); Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { public void run() { Integer number = NumberMachine.getInstance().getExpressManager().generateNewManager(); System.out.println(number + "号快速客户等待服务"); } }, 0L, (long)(Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6), TimeUnit.SECONDS); Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { public void run() { Integer number = NumberMachine.getInstance().getVipManager().generateNewManager(); System.out.println(number + "号VIP客户等待服务"); } }, 0L, (long)(Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2), TimeUnit.SECONDS); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值