/*
(四)ServiceWindow类
1.定义一个start方法,内部启动一个线程,根据服务窗口的类别分别循环调用三个不同的方法。
2.定义三个方法分别对三种客户进行服务,为了观察运行效果,应详细打印出其中的细节信息。
源码如下:*/
public class ServiceWindow {
private CustomerType type = CustomerType.COMMON;//因为只有3个元素,所以用枚举
private int number = 1;
public int getNumber() {
return number;
}
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(true)//循环去NumberMachine窗口要任务
{
switch(type)
{
case COMMON://普通窗口
commonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
public void vipService() {
String windoName3 = "第"+number+"号VIP窗口";
System.out.println(windoName3+"开始获取VIP客户任务");
Integer isNumber3 = NumberMachine.getInstance().getVipMansger().fetchNumber();
if(isNumber3!=null)
{
System.out.println(windoName3+"现在为第"+isNumber3+"个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(windoName3+"为第"+isNumber3+"号VIP客户服务时间"+serviceTime/1000+"秒");
}
else
{
System.out.println(windoName3+"没有取得VIP客户号");
commonService();
}
}
public void expressService() {
String windoName2 = "第"+number+"号快速窗口";
System.out.println(windoName2+"开始获取快速客户任务");
Integer isNumber2 = NumberMachine.getInstance().getExpressMansger().fetchNumber();
if(isNumber2!=null)
{
System.out.println(windoName2+"现在为第"+isNumber2+"个快速客户服务");
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(windoName2+"为第"+isNumber2+"号快速客户服务时间"+serviceTime/1000+"秒");
}
else
{
System.out.println(windoName2+"没有取得快速客户号");
commonService();
}
}
public void commonService() {
String windoName1 = "第"+number+"号"+type+"窗口";
System.out.println(windoName1+"开始获取普通客户任务");
Integer isNumber1 = NumberMachine.getInstance().getCommonMansger().fetchNumber();
if(isNumber1!=null)
{
System.out.println(windoName1+"现在为第"+isNumber1+"个普通客户服务");
int maxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
/* public int nextInt()
返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值*/
int serviceTime = new Random().nextInt(maxRandom)+1+Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(windoName1+"为第"+isNumber1+"号普通客户服务时间"+serviceTime/1000+"秒");
}
else
{
System.out.println(windoName1+"没有取得普通客户号");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
);
}
}
package com.isoftstone.interview.bank;
/*(五)Constants类
1.定义三个常量:MAX_SERVICE_TIME、MIN_SERVICE_TIME、COMMON_CUSTOMER_INTERVAL_TIME。
源码如
*/
public class Constants {
//MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
public static int MAX_SERVICE_TIME = 10000;
public static int MIN_SERVICE_TIME = 1000;
public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;
}
package com.isoftstone.interview.bank;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MainClass {
/**
* @param args
*/
/*(六 )MainClass类
1.用for循环创建出4个普通窗口,再创建出1个快速窗口和一个VIP窗口。
2.接着再创建三个定时器,分别定时去创建新的普通客户号码、新的快速客户号码、新的VIP客户号码。
源码如下:*/
public static void main(String[] args) {
// ServiceWindow window = null;
//产生4个普通窗口
for(int i=1;i<5;i++){
ServiceWindow window = new ServiceWindow();
window.setType(CustomerType.COMMON);
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 = NumberMachine.getInstance().getCommonMansger().generateNewManager();
//logger.info("第" + serviceNumber + "号普通客户正在等待服务!");
System.out.println("第" + serviceNumber + "号普通客户正在等待服务!");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
//快速客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer serviceNumber = NumberMachine.getInstance().getExpressMansger().generateNewManager();
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 = NumberMachine.getInstance().getVipMansger().generateNewManager();
System.out.println("第" + serviceNumber + "号VIP客户正在等待服务!");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6,
TimeUnit.SECONDS);
}
}