银行业务调度系统-java并发程序模拟解决方案
来源:互联网 发布:excel数据库管理功能 编辑:程序博客网 时间:2024/05/16 15:00
题目地址:http://blog.csdn.net/ie800/article/details/19376439
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()); }}
0 0
- 银行业务调度系统-java并发程序模拟解决方案
- 模拟银行业务调度系统
- 模拟银行业务调度系统
- 用java模拟银行业务调度系统
- 银行业务调度系统模拟实现
- 模拟银行业务中取号调度系统
- java--银行业务调度系统
- Java 银行业务调度系统
- java银行业务调度系统
- java 银行业务调度系统
- java、银行业务调度系统
- java--银行业务调度系统
- (JAVA)银行业务调度系统
- java--银行业务调度系统
- java--银行业务调度系统
- java 银行业务调度系统
- java(银行业务调度系统 )
- 黑马程序-----银行业务调度系统
- TabHost+ViewPager做能左右滑动的TabHost
- 如何用计划任务执行powershell脚本
- SQL常用命令
- 人以负熵为食
- 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!
- 银行业务调度系统-java并发程序模拟解决方案
- Core Data浅谈系列之一 : 基础结构
- makefile的语法及写法(一)
- 使用git 新建分支以及管理分支
- 回车直接实现点击某按钮的效果
- 2014年2月27日 mybatis 的 association 和 collection 的用法小结
- HTML5的head标签介绍
- error C2598: linkage specification must be at global scope 一个意外
- makefile的语法及写法(二)