思路:
到银行处理业务的步骤为: 先到取号机取号,服务器叫号,服务,服务完毕
银行服务窗口办理的业务可分为普通业务,快速业务,VIP业务
办理普通业务,快速业务,VIP业务的比例分别为6:3:1;
根据分析转换成JAVA对象
创建一个号码管理,来一个客户往里添加一个号码,服务台处理一个用户,从号码管理器中删除一个号码
创建一个号码管理的机器,里面可以创建VIP业务,快速业务,普通业务相应的号码管理器,并设置get方法取得相应的管理器,由于只有一台机器管理有的业务类型,所以此类为单例的类。
创建一个服务台类,
1、创建一个方法开启服务,当服务器服务开启以后,就可以来一个客户,往相应类型里面添加一个号码。
2、创建每个业务的执行类,传入相应的窗口类型,如果快速窗口和VIP窗口当前没有客户,则等一秒后取普通窗口的客户服务
3、创建一个枚举类型,并设置VIP,快速,普通类型
4、创建一个线程池,并执行while(true),并判断当前业务类型,让当前服务窗口不间断的服务。
5、创建一个主要调用的类。创建窗口对象,调用开始服务窗口,这时不断的用号码管理器中添加相应业务的号码
6、在主要调用的类中创建普通,快速,VIP窗口,并调用开始办理业务的方法;
示例代码:
先创建一个号码管理器
package bank;
import java.util.ArrayList;
import java.util.List;
public class NumManager {
private List<Integer> numList = new ArrayList();
private int i = 0;
public synchronized Integer numAdd() {
i++;
numList.add(i);
return i;
}
public synchronized Integer removeAdd() {
if (numList.size() > 0) {
return numList.remove(0);
}
return null;
}
}
创建一个号码管理台,管理所有业务的号码管理器
package bank;
public class NumControllMachine {
NumManager vipNumManager = new NumManager();
NumManager expressNumManager = new NumManager();
NumManager commonNumManager = new NumManager();
public NumManager getVipNumManager() {
return vipNumManager;
}
public NumManager getExpressNumManager() {
return expressNumManager;
}
public NumManager getCommonNumManager() {
return commonNumManager;
}
private NumControllMachine() {
}
private static NumControllMachine numControlMachine = new NumControllMachine();
public static NumControllMachine getInstance() {
return numControlMachine;
}
}
创建一个枚举类型,并创建相应的对象
package bank;
public enum BusinessType {
COMMON, EXPRESS, VIP;
public String toString() {
switch (this) {
case COMMON:
return "普通";
case EXPRESS:
return "快速";
case VIP:
return "VIP";
default:
return null;
}
};
}
创建时间管理类,并建立全局参数(最大服务时间,最短服务时间《快速业务为最短服务时间》)
package bank;
public class ServiceTime {
public static int MAX_SERVICE_TIME = 6000;
public static int MIN_SERVICE_TIME = 1000;
}
建立服务窗口类
package bank;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ServerWindow {
private NumControllMachine numcontrollMachine = NumControllMachine
.getInstance();
private int windowId;
public ServerWindow(int windowId, BusinessType type) {
super();
this.windowId = windowId;
this.type = type;
}
private BusinessType type;
/*
*根据各窗口办理什么业务,调用相应的方法
*/
public void callNum() {
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
switch (type) {
case COMMON:
commonServer(type);
break;
case EXPRESS:
expressServer(type);
break;
case VIP:
vipServer(type);
break;
}
}
}
});
}
public void commonServer(BusinessType type) {
Integer removeClicentId = numcontrollMachine.getCommonNumManager()
.removeAdd();
if (removeClicentId == null) {
System.out.println(windowId + "号" + type + "窗口没有取到"
+ BusinessType.COMMON + "任务,等待1秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
try {
System.out.println(windowId + "号" + type + "窗口开始为办理"
+ BusinessType.COMMON + "业务的" + removeClicentId
+ "号客户服务");
long startTime = System.currentTimeMillis();
int randomTime = ServiceTime.MIN_SERVICE_TIME
+ (ServiceTime.MAX_SERVICE_TIME - ServiceTime.MIN_SERVICE_TIME);
Thread.sleep(new Random().nextInt(randomTime));
long winSerTime = (System.currentTimeMillis() - startTime) / 1000;
System.out.println(windowId + "号" + type + "窗口开始为办理"
+ BusinessType.COMMON + "业务的" + removeClicentId
+ "号客户服务完毕,服务时间为" + winSerTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void expressServer(BusinessType type) {
Integer removeClicentId = numcontrollMachine.getExpressNumManager()
.removeAdd();
if (removeClicentId == null) {
try {
System.out.println(windowId + "号" + type + "窗口没有取到"
+ BusinessType.EXPRESS + "任务,等待1秒");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
commonServer(type);
} else {
try {
System.out.println(windowId + "号" + type + "窗口开始为办理"
+ BusinessType.EXPRESS + "业务的" + removeClicentId
+ "号客户服务");
long startTime = System.currentTimeMillis();
Thread.sleep(ServiceTime.MIN_SERVICE_TIME);
long winSerTime = (System.currentTimeMillis() - startTime) / 1000;
System.out.println(windowId + "号" + type + "窗口开始为办理"
+ BusinessType.EXPRESS + "业务的" + removeClicentId
+ "号客户服务完毕,服务时间为" + winSerTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void vipServer(BusinessType type) {
Integer removeClicentId = numcontrollMachine.getVipNumManager()
.removeAdd();
if (removeClicentId == null) {
try {
System.out.println(windowId + "号" + type + "窗口没有取到"
+ BusinessType.VIP + "任务,等待1秒");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
commonServer(type);
} else {
try {
System.out.println(windowId + "号" + type + "窗口开始为办理"
+ BusinessType.VIP + "业务的" + removeClicentId + "号客户服务");
long startTime = System.currentTimeMillis();
int randomTime = ServiceTime.MIN_SERVICE_TIME
+ (ServiceTime.MAX_SERVICE_TIME - ServiceTime.MIN_SERVICE_TIME);
Thread.sleep(randomTime);
long winSerTime = (System.currentTimeMillis() - startTime) / 1000;
System.out.println(windowId + "号" + type + "窗口开始为办理"
+ BusinessType.VIP + "业务的" + removeClicentId
+ "号客户服务完毕,服务时间为" + winSerTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
*开启服务用号码管理器中添加新来的用户
*/
public void start() {
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int addClientId = numcontrollMachine.getCommonNumManager()
.numAdd();
System.out.println("办理" + BusinessType.COMMON + "业务 的"
+ addClientId + "号客户等待服务");
}
}
});
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(new Random().nextInt(8)*1000+1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int addClientId = numcontrollMachine.getExpressNumManager()
.numAdd();
System.out.println("办理" + BusinessType.EXPRESS + "业务 的"
+ addClientId + "号客户等待服务");
}
}
});
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(new Random().nextInt(17) * 1000 + 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int addClientId = numcontrollMachine.getVipNumManager()
.numAdd();
System.out.println("办理" + BusinessType.VIP + "业务 的"
+ addClientId + "号客户等待服务");
}
}
});
}
}
建立调用的类:开启各个服务窗口,并开始服务
package bank;
public class MainService {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
ServerWindow comSerWin = new ServerWindow(i, BusinessType.COMMON);
comSerWin.start();
comSerWin.callNum();
}
ServerWindow expressWin = new ServerWindow(6, BusinessType.EXPRESS);
expressWin.start();
expressWin.callNum();
ServerWindow vipWin = new ServerWindow(7, BusinessType.VIP);
vipWin.start();
vipWin.callNum();
}
}
---------------------- ASP.Net+Android+IO开发 Net培训期待与您交流! ----------------------
详细请查看http://edu.csdn.net