今天真的让我激动不已,当看完了张孝祥老师的视频之后觉得视频里张老师的方法不是很妥当,因为客户都是固定每1秒一个普通客户,每两秒一个快速客户,每6秒一个VIP客户,这种方法过于死板,不能充分体现现实的随机性,也就是说,可能一天都是普通客户,也可能一般的客户都是VIP客户,这可怎么办呢?经过我一下午的努力,终于解决了随机性的问题。这就分享我的代码! 首先我的思路是将客户进入银行的时候目的就很明确,直接从提号机提到自己的身份代号,把提号机和号码管理合为一体,并通过随机来决定客户的身份,随机的比率按照VIP用户:快速用户:普通用户=1:3:6,也就是用随机产生10个数,0为VIP用户,1-3为快速用户,4-9为普通用户,而且把身份代号和用户的编号存到集合中,这就要新建一个Custom(int num ,String level)对象,并写get方法,将Custom存到集合中。具体代码如下:
public class Custom {
private int serviceNumber;
private String level;
public Custom() {
}
public Custom(int serviceNumber,String level){
this.serviceNumber=serviceNumber;
this.level=level;
}
public int getServiceNumber() {
return serviceNumber;
}
public String getLevel() {
return level;
}
}
public class NumberManager {
private NumberManager(){}
private static NumberManager instance = new NumberManager();
public static NumberManager getInstance(){
return instance;
}
private static String level;
public static String getLevel() {
return level;
}
private int lastNumber =0;
private int commenNumber =0;
private int expressNumber =0;
private int vipNumber =0;
private List<Custom> queueNumbers = new ArrayList<Custom>();
public synchronized Integer generateNewNumber(){
int stand =new Random().nextInt(10);
switch(stand){
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
level="普通";
lastNumber=++commenNumber;
break;
case 1:
case 2:
case 3:
level="快速";
lastNumber=++expressNumber;
break;
case 0:
level="VIP";
lastNumber=++vipNumber;
break;
}
Custom cus =new Custom(lastNumber,level);
queueNumbers.add(cus);
return lastNumber;
public enum CustomerType {
COMMON,EXPRESS,VIP;
public String toString(){
String name = null;
switch(this){
case COMMON:
name = "普通";
break;
case EXPRESS:
name = "快速";
break;
case VIP:
name = name();
break;
}
return name;
}
}
接着创建一个常量的类:
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 ServiceWindow {
private int number = 1;
private CustomerType type = CustomerType.COMMON;
public CustomerType getType() {
return type;
}
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){
switch(type){
case COMMON:
commonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
});
}
private void commonService() {
String windowName = "第" + number + "号" + type + "窗口";//这里也没放同步锁,防止死锁
System.out.println(windowName + "正在获取任务");
Integer lastNumber=NumberManager.getInstance().fetchCommenNumber();
if(lastNumber!=null){
System.out.println(windowName + "为第"+ lastNumber+ "个普通客户服务");
int maxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
int serviceTime = new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME;
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<5;i++){
ServiceWindow window = new ServiceWindow();
window.setNumber(i);
window.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 lastNumber=NumberManager.getInstance().generateNewNumber();