银行业务调度系统-java并发程序模拟解决方案
2014-02-27 17:09
525 查看
题目地址:/article/1340010.html
import java.util.HashMap; import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; public class Test { // protected Logger log = Logger.getLogger(this.getClass()); // 号码生成封装Map private Map<CustomerType, AtomicInteger> nm = new HashMap<CustomerType, AtomicInteger>(); { nm.put(CustomerType.COMMON, new AtomicInteger(1)); nm.put(CustomerType.EXPRESS, new AtomicInteger(1)); nm.put(CustomerType.VIP, new AtomicInteger(1)); } // 排号队列封装Map private Map<CustomerType, BlockingQueue<Integer>> qm = new HashMap<CustomerType, BlockingQueue<Integer>>(); { qm.put(CustomerType.COMMON, new LinkedBlockingQueue<Integer>()); qm.put(CustomerType.EXPRESS, new LinkedBlockingQueue<Integer>()); qm.put(CustomerType.VIP, new LinkedBlockingQueue<Integer>()); } // 呼叫应答模拟队列 private BlockingQueue<Semaphore> crq = new LinkedBlockingQueue<Semaphore>(); enum CustomerType { COMMON, EXPRESS, VIP; public String toString() { switch (this) { case COMMON: return "【普通】"; case EXPRESS: return "【快速】"; } return "【VIP】"; } } /** * 服务窗口 */ class Service extends Thread { private CustomerType[] types; private String name; // 信号量媒介--一次只能服务一人 private Semaphore sem = new Semaphore(1); public Service(CustomerType[] types, String name) { this.types = types; this.name = name; } public void run() { int length = types.length; int i = 0;//如果要持续工作套入 while(true) i=0;重置代码 while (i < length) { CustomerType type = types[i]; BlockingQueue<Integer> q = qm.get(type); try { Integer num = q.poll(100, TimeUnit.MILLISECONDS); if (num != null) { // 呼叫signal-call sem.acquire(); System.out.println(this.name + "正在呼叫~" + num + "~号" + type.toString() + "!"); crq.put(sem); // 等待服务wait sem.acquire(); // 成功继续 System.out.println(this.name + "开始为~" + num + "~号" + type.toString() + "服务!"); // 服务中 --多步操作完全可以用线程模拟 Thread.sleep(5000); // 服务完毕 System.out.println(this.name + "为~" + num + "~号" + type.toString() + "服务完毕!"); sem.release(); //数组次序确定优先级 i = 0; } else { i++; } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 生成客户 */ class GenerateCustomer extends Thread { private CustomerType type; public GenerateCustomer(CustomerType type) { this.type = type; } public void run() { while (true) { try { AtomicInteger num = nm.get(type); int i = num.getAndIncrement(); BlockingQueue<Integer> q = qm.get(type); System.out.println(i + "~号" + type.toString() + "领号等待!"); q.put(i); // 每隔3s生成一个客户 Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // } } } /** * 模拟应答-无记名 */ class CallRes extends Thread { public void run() { while (true) { try { Semaphore sem = crq.take(); // 响应一下,释放许可 sem.release(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * Purpose: * * @author maomj * @createTime: 2014-2-27 上午9:51:35 * @createSpecification: * @modifiedTime: * @modifiedBy * @modifiedSpecification: * @param args */ public static void main(String[] args) { Test test=new Test(); ExecutorService threadPool = Executors.newFixedThreadPool(18); // 创建服务窗口 // 1-4 threadPool.execute(test.new Service(new CustomerType[]{CustomerType.COMMON},"【窗口1】")); threadPool.execute(test.new Service(new CustomerType[]{CustomerType.COMMON},"【窗口2】")); threadPool.execute(test.new Service(new CustomerType[]{CustomerType.COMMON},"【窗口3】")); threadPool.execute(test.new Service(new CustomerType[]{CustomerType.COMMON},"【窗口4】")); // 5 threadPool.execute(test.new Service(new CustomerType[]{CustomerType.EXPRESS,CustomerType.COMMON},"【窗口5】")); // 6 threadPool.execute(test.new Service(new CustomerType[]{CustomerType.VIP,CustomerType.COMMON},"【窗口6】")); // 生成客户1:6:3 threadPool.execute(test.new GenerateCustomer(CustomerType.VIP)); for(int i=0;i<6;i++) threadPool.execute(test.new GenerateCustomer(CustomerType.COMMON)); for(int i=0;i<3;i++) threadPool.execute(test.new GenerateCustomer(CustomerType.EXPRESS)); //启动模拟应答 threadPool.execute(test.new CallRes()); } }
相关文章推荐
- java.io.IOException: open failed: EINVAL (Invalid argument)
- Java 性能优化之 String 篇
- 利用JAXB进行xml和javabean之间转换
- 关于线程(二)线程的状态
- 菜鸟学习Struts——bean标签库
- 菜鸟学习Struts——bean标签库
- java个人学习笔记02(注释+数据类型+算术逻辑运算)
- spring 动态创建bean
- Java Thread BLOCKED和WAITING两种状态的区别(英)
- eclispe中只能输入英文,无法调用输入法
- eclipse创建2dx项目
- thinking in java -- notes-2
- Spring AOP
- JAVA 数据类型的范围
- Java Web应用开发中的一些概念(转载)
- 关于线程(一)线程的基础
- ubuntu12.04 安装java1.7
- JAVA 实现 EchoServer EchoClient 服务器、客户机本地通信程序
- JAVA 实现 EchoServer EchoClient 服务器、客户机本地通信程序
- JAVA程序内存结构