------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
有三种对应类型的客户:VIP客户,普通客户,快速客户 ,异步随机生成各种类型的客户,各类型客户在其对应窗口按顺序依次办理业务 。
本系统一共要产生三个号码管理器对象,各自管理一类用户的排队号码。这三个号码管理器对象统一由一个号码机器进行管理,这个号码机器在整个系统中始终只能有一个,所以,它要被设计成单例。
各类型客户在其对应窗口按顺序依次办理业务即叫号.
NumberManager类
定义一个用于存储上一个客户号码的成员变量和用于存储所有等待服务的客户号码的队列集合
- 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 fetchServiceNumber(){
- Integer number=null;
- if(queueNumber.size()>0){
- number= queueNumber.remove(0);
- }
- return number;
- }
- }
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 fetchServiceNumber(){
Integer number=null;
if(queueNumber.size()>0){
number= queueNumber.remove(0);
}
return number;
}
}
NumberMachine类
定义三个成员变量分别指向三个NumberManager对象,分别表示普通、快速和VIP客户的号码管理器,定义三个对应的方法来返回这三个NumberManager对象。
- public class NumberMachine {
- private NumberManager commonManager=new NumberManager();
- private NumberManager vipManager=new NumberManager();
- private NumberManager expressManager=new NumberManager();
- public NumberManager getCommonManager() {
- return commonManager;
- }
- public NumberManager getVipManager() {
- return vipManager;
- }
- public NumberManager getExpressManager() {
- return expressManager;
- }
- private NumberMachine(){}
- private static NumberMachine instance=new NumberMachine();
- public static NumberMachine getInstance(){
- return instance;
- }
- }
public class NumberMachine {
private NumberManager commonManager=new NumberManager();
private NumberManager vipManager=new NumberManager();
private NumberManager expressManager=new NumberManager();
public NumberManager getCommonManager() {
return commonManager;
}
public NumberManager getVipManager() {
return vipManager;
}
public NumberManager getExpressManager() {
return expressManager;
}
private NumberMachine(){}
private static NumberMachine instance=new NumberMachine();
public static NumberMachine getInstance(){
return instance;
}
}
CustomerType枚举类
系统中有三种类型的客户,所以用定义一个枚举类,其中定义三个成员分别表示三种类型的客户
- public enum CustomerType {
- COMMON,EXPRESS,VIP;
- public String toString(){
- switch(this){
- case COMMON:
- return "普通";
- case EXPRESS:
- return "快速";
- case VIP:
- return "VIP";
- }
- return null;
- }
- }
public enum CustomerType {
COMMON,EXPRESS,VIP;
public String toString(){
switch(this){
case COMMON:
return "普通";
case EXPRESS:
return "快速";
case VIP:
return "VIP";
}
return null;
}
}
ServiceWindow类
定义一个start方法,内部启动一个线程,根据服务窗口的类别分别循环调用三个不同的方法。
- 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){
- //NumberMachine.getInstance().getCommonManager();
- switch(type){
- case COMMON:
- commonService();
- case EXPRESS:
- expressService();
- case VIP:
- vipService();
- }
- }
- }
- }
- );
- }
- private void commonService(){
- String windowName="第"+windowId+"号"+type+"窗口";
- System.out.println(windowName+"正在取号");
- Integer number=NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
- if(number!=null){
- System.out.println(windowName+"为第"+number+"个普通客户服务");
- long beginTime=System.currentTimeMillis();
- int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
- long serverTime=new Random().nextInt(maxRandom) +1+Constants.MIN_SERVICE_TIME;
- try {
- Thread.sleep(serverTime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- long costTime=System.currentTimeMillis()-beginTime;
- System.out.println(windowName+"为第"+number+"个普通客户完成服务,耗时"+costTime/1000+"秒");
- }else{
- System.out.println("没有取到任务,先休息一秒钟!");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private void expressService(){
- String windowName="第"+windowId+"号"+type+"窗口";
- System.out.println(windowName+"正在取号");
- Integer number=NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
- if(number!=null){
- System.out.println(windowName+"为第"+number+"个"+type+"客户服务");
- long beginTime=System.currentTimeMillis();
- //int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
- //long serverTime=new Random().nextInt(maxRandom) +1+Constants.MIN_SERVICE_TIME;
- 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+"窗口";
- System.out.println(windowName+"正在取号");
- Integer number=NumberMachine.getInstance().getVipManager().fetchServiceNumber();
- if(number!=null){
- System.out.println(windowName+"为第"+number+"个"+type+"客户服务");
- long beginTime=System.currentTimeMillis();
- int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
- long serverTime=new Random().nextInt(maxRandom) +1+Constants.MIN_SERVICE_TIME;
- try {
- Thread.sleep(serverTime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- long costTime=System.currentTimeMillis()-beginTime;
- System.out.println(windowName+"为第"+number+"个"+type+"客户完成服务,耗时"+costTime/1000+"秒");
- }else{
- System.out.println(windowName+"没有取到任务!");
- commonService();
- }
- }
- }
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){
//NumberMachine.getInstance().getCommonManager();
switch(type){
case COMMON:
commonService();
case EXPRESS:
expressService();
case VIP:
vipService();
}
}
}
}
);
}
private void commonService(){
String windowName="第"+windowId+"号"+type+"窗口";
System.out.println(windowName+"正在取号");
Integer number=NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
if(number!=null){
System.out.println(windowName+"为第"+number+"个普通客户服务");
long beginTime=System.currentTimeMillis();
int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serverTime=new Random().nextInt(maxRandom) +1+Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serverTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;
System.out.println(windowName+"为第"+number+"个普通客户完成服务,耗时"+costTime/1000+"秒");
}else{
System.out.println("没有取到任务,先休息一秒钟!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void expressService(){
String windowName="第"+windowId+"号"+type+"窗口";
System.out.println(windowName+"正在取号");
Integer number=NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
if(number!=null){
System.out.println(windowName+"为第"+number+"个"+type+"客户服务");
long beginTime=System.currentTimeMillis();
//int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
//long serverTime=new Random().nextInt(maxRandom) +1+Constants.MIN_SERVICE_TIME;
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+"窗口";
System.out.println(windowName+"正在取号");
Integer number=NumberMachine.getInstance().getVipManager().fetchServiceNumber();
if(number!=null){
System.out.println(windowName+"为第"+number+"个"+type+"客户服务");
long beginTime=System.currentTimeMillis();
int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serverTime=new Random().nextInt(maxRandom) +1+Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serverTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;
System.out.println(windowName+"为第"+number+"个"+type+"客户完成服务,耗时"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到任务!");
commonService();
}
}
}
Constants类
定义三个常量:MAX_SERVICE_TIME、MIN_SERVICE_TIME、COMMON_CUSTOMER_INTERVAL_TIME
- 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;
- }
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;
}
MainClass类
用for循环创建出4个普通窗口,再创建出1个快速窗口和一个VIP窗口。
接着再创建三个定时器,分别定时去创建新的普通客户号码、新的快速客户号码、新的VIP客户号码。
- 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();
- 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);
- }
- }
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();
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);
}
}