高薪面试题01—银行业务调度系统

 

 

package com.isoftstone.interview.bank;

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

public class NumberManager {
	private int lastNumber = 1;
	private List<Integer> queueNumber = new ArrayList<Integer>();
	public synchronized Integer generateNewManager(){//产生新号码
		queueNumber.add(lastNumber);//把号码存入集合中
		return lastNumber++;
	}
	
	public synchronized Integer fetchServerNumber(){
		Integer number = null;
		if(queueNumber.size()>0){
			number = queueNumber.remove(0);//取出集合中的元素
		}
		return number;//返回号码
	}
}
package com.isoftstone.interview.bank;

public class NumberMachine {
	private NumberManager commonManager = new NumberManager();
	private NumberManager expressManager = new NumberManager();
	private NumberManager vipManager = new NumberManager();
	
	public NumberManager getCommonManager() {
		return commonManager;
	}
	public NumberManager getExpressManager() {
		return expressManager;
	}
	public NumberManager getVipManager() {
		return vipManager;
	}

	private NumberMachine(){};
	private static NumberMachine instance = new NumberMachine();
	public static NumberMachine getInstance(){
		return instance;
	}
	
}

 

package com.isoftstone.interview.bank;

public enum CustomerType {
	COMMON,EXPRESS,VIP;
	
	public String toString(){
		switch(this){
		case COMMON:
			return "普通";
		case EXPRESS:
			return "快速";
		case VIP:
			return name();
		}
		return null;
	}
}

 

package com.isoftstone.interview.bank;

import java.util.Random;
import java.util.concurrent.Executors;

public class ServiceWindow {
	private CustomerType type = CustomerType.COMMON;
	private int windowId = 1;
	
	//只获取,因为是变化的
	public void setType(CustomerType type) {
		this.type = type;
	}

	public void setWindowId(int windowId) {
		this.windowId = windowId;
	}

	public void start(){
		//线程池,只有一个线程
		Executors.newSingleThreadExecutor().execute(new Runnable(){
			public void run(){
				while(true){
					switch(type){
						case COMMON:
						commonService();
							break;
						case EXPRESS:
							expressService();
							break;
						case VIP:
							vipService();
							break;
					}

				}
			}
		});
	}
	
	private void commonService() {
		String windowName = "第"+windowId+"号"+type+"窗口";
		
		Integer number = NumberMachine.getInstance().getCommonManager().fetchServerNumber();
		System.out.println(windowName+"正在获取任务");
		if(number != null){
			System.out.println(windowName+"为第"+number+"个"+"普通"+"客户服务");
			long beginTime = System.currentTimeMillis();
			//最大随机数9秒
			int maxRand = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
			long serveTime = new Random().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;//随机数在1到9秒
			try {
				Thread.sleep(serveTime);//普通窗口服务时间
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			long costTime = System.currentTimeMillis()-beginTime;
			System.out.println(windowName+"为第"+number+"个"+"普通"+"客户完成服务,耗时"+costTime/1000+"秒");
		}else{
			System.out.println(windowName+"没有取到服务任务,先休息一秒钟");
			try {
				Thread.sleep(1000);//暂停1秒
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void expressService() {
		String windowName = "第"+windowId+"号"+type+"窗口";
		
		Integer number = NumberMachine.getInstance().getExpressManager().fetchServerNumber();
		System.out.println(windowName+"正在获取任务");
		if(number != null){
			long beginTime = System.currentTimeMillis();
			//最大随机数9秒
			//int maxRand = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
			//long serveTime = new Random().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;//随机数在1到9秒
			try {
				Thread.sleep(Constants.MIN_SERVICE_TIME);//快速窗口服务时间
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			long costTime = System.currentTimeMillis()-beginTime;
			System.out.println(windowName+"为第"+number+"个"+type+"客户完成服务,耗时"+costTime/1000+"秒");
		}else{
			System.out.println(windowName+"没有取到服务任务!");
			commonService();//快速窗口没有任务,就看看普通窗口有没有任务
		}
	}
	
	private void vipService() {
		String windowName = "第"+windowId+"号"+type+"窗口";
		
		//阻塞式方法
		Integer number = NumberMachine.getInstance().getVipManager().fetchServerNumber();
		System.out.println(windowName+"正在获取任务");
		if(number != null){
			long beginTime = System.currentTimeMillis();
			//最大随机数9秒
			int maxRand = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
			long serveTime = new Random().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;//随机数在1到9秒
			try {
				Thread.sleep(serveTime);//服务时间
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			long costTime = System.currentTimeMillis()-beginTime;
			System.out.println(windowName+"为第"+number+"个"+type+"客户完成服务,耗时"+costTime/1000+"秒");
		}else{
			System.out.println(windowName+"没有取到服务任务!");
			commonService();//vip窗口没有任务,就看看普通窗口有没有任务
		}
	}
}

 

package com.isoftstone.interview.bank;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MainClass {

	public static void main(String[] args) {
		
		for(int i=1; i<5; i++){//启动四个普通客户窗口
			ServiceWindow commonWindow = new ServiceWindow();
			commonWindow.setWindowId(i);
			commonWindow.start();
		}
		
		ServiceWindow expressWindow = new ServiceWindow();
		expressWindow.setType(CustomerType.EXPRESS);
		expressWindow.start();//启动一个快速窗口
		
		ServiceWindow vipWindow = new ServiceWindow();
		vipWindow.setType(CustomerType.VIP);
		vipWindow.start();//启动一个vip窗口
		
		//线程池,一个定时的线程,可以一定频率连续启动
		Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
				new Runnable(){
					public void run(){
						Integer number = NumberMachine.getInstance().getCommonManager().generateNewManager();
						System.out.println(number+"号普通客户等待服务");
					}
				},
				0,//初次启动时间
				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+"号快速客户等待服务");
					}
				},
				0,//初次启动时间
				Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2, //间隔多长时间
				TimeUnit.SECONDS //间隔时间的单位
			);
		
		//线程池,一个定时的线程,可以一定频率连续启动
		Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
				new Runnable(){
					public void run(){
						Integer number = NumberMachine.getInstance().getVipManager().generateNewManager();
						System.out.println(number+"号vip客户等待服务");
					}
				},
				0,//初次启动时间
				Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6, //间隔多长时间
				TimeUnit.SECONDS //间隔时间的单位
			);
		

	}

}

 

package com.isoftstone.interview.bank;

public class Constants {
	public static int MAX_SERVICE_TIME = 10000;
	public static int MIN_SERVICE_TIME = 1000;
	public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值