首页 > 代码库 > 关于多线程-银行出纳问题
关于多线程-银行出纳问题
题目如下:
/* 模拟实现银行业务调度系统逻辑,具体需求如下: 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。 异步随机生成各种类型的客户,生成各类型用户的概率比例为: VIP客户:普通客户:快速客户 = 1 :6 :3。 客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。 各类型客户在其对应窗口按顺序依次办理业务。 当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。 随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。 不要求实现GUI,只考虑系统逻辑实现,可通过打印的方式展现程序运行结果。 */
这个问题的解决借鉴了Java编程思想里面关于银行出纳仿真的小节,主要是直接使用了线程安全的容器ArrayBlockingQueue。代码如下:
Customer.java 本源文件定义了三种客户,以及枚举类型的客户类型。
package banktellersimulation; enum CUSTOMER{ VIP, //1 ORDINARY, //6 QUICK //3 } public class Customer { private final CUSTOMER id; private final int servicetime; public Customer(CUSTOMER customer){ if (customer == CUSTOMER.VIP){ servicetime = 500; } else if (customer == CUSTOMER.ORDINARY){ servicetime = 300; } else { servicetime = 100; } id = customer; } public int getServiceTime(){ return servicetime; } public String toString(){ return id.toString() + ":" + getServiceTime(); } }CustomerGenerator.java是一个生产者模型,即向相应的容器中添加客户——消费者。
package banktellersimulation; import java.util.ArrayList; import java.util.Random; import java.util.concurrent.ArrayBlockingQueue; import com.sun.org.apache.xalan.internal.xsltc.compiler.sym; public class CustomerGenerator implements Runnable{ private ArrayBlockingQueue<Customer> customerlinevip; private ArrayBlockingQueue<Customer> customerlineoridinary; private ArrayBlockingQueue<Customer> customerlinequick; private Random rand = new Random(); private double[] data = http://www.mamicode.com/new double[]{0.0, 0.1, 0.7, 1.0};>
Window.java模拟银行的窗口类package banktellersimulation; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; public class Window implements Runnable{ private final int id; private ArrayBlockingQueue<Customer> customerlinevip; private ArrayBlockingQueue<Customer> customerlineoridinary; private ArrayBlockingQueue<Customer> customerlinequick; public Window(int id, ArrayBlockingQueue<Customer> customerlinevip, ArrayBlockingQueue<Customer> customerlineoridinary, ArrayBlockingQueue<Customer> customerlinequick){ this.customerlinevip = customerlinevip; this.customerlineoridinary = customerlineoridinary; this.customerlinequick = customerlinequick; this.id = id; } @Override public void run() { try{ while (!Thread.interrupted()){ Customer temp = null; if (id >= 1 && id <= 4){ temp = customerlineoridinary.take(); TimeUnit.MICROSECONDS.sleep(temp.getServiceTime()); } else if (id == 5){ if (customerlinequick.size() == 0 && customerlineoridinary.size() != 0){ temp = customerlineoridinary.take(); TimeUnit.MICROSECONDS.sleep(temp.getServiceTime()); } else { temp = customerlinequick.take(); TimeUnit.MICROSECONDS.sleep(temp.getServiceTime()); } } else if (id == 6){ if (customerlinevip.size() == 0 && customerlineoridinary.size() != 0){ temp = customerlineoridinary.take(); TimeUnit.MICROSECONDS.sleep(temp.getServiceTime()); } else { temp = customerlinevip.take(); TimeUnit.MICROSECONDS.sleep(temp.getServiceTime()); } } System.out.println(temp + "deal by:" + this); } }catch (InterruptedException e) { e.printStackTrace(); } } public String toString(){ return id + ""; } }
Bank.java测试类,线程运行的主函数
package banktellersimulation; import java.util.concurrent.ArrayBlockingQueue; public class Bank { public static void main(String[] args){ ArrayBlockingQueue<Customer> customerlinevip = new ArrayBlockingQueue<Customer>(20); ArrayBlockingQueue<Customer> customerlineoridinary = new ArrayBlockingQueue<Customer>(20); ArrayBlockingQueue<Customer> customerlinequick = new ArrayBlockingQueue<Customer>(20); new Thread(new CustomerGenerator(customerlinevip, customerlineoridinary, customerlinequick)).start(); for (int i = 1; i <=6 ;i++){ new Thread(new Window(i, customerlinevip, customerlineoridinary, customerlinequick)).start(); } } }代码纯粹个人编写,水平有限如果有什么错误,欢迎大家扶正。关于多线程-银行出纳问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。