A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李铁 中级黑马   /  2012-10-27 23:08  /  1540 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李铁 于 2012-10-28 11:22 编辑

为什么我运行了代码,却没有发现有vip窗口为普通客户服务的呢?求大神指正!
代码如下:
public class NumberManager {
private int lastNumber=1;
//保存正在排队的号码
private List<Integer> queueNumber=new ArrayList<Integer>();
// 以下两个方法同时访问一个资源,所以要让它们互斥;加上锁。
public synchronized Integer  generateManager(){
  queueNumber.add(lastNumber);
  return lastNumber++;
}

public synchronized Integer fctchServiceNumber(){
  Integer number=null;
  if(queueNumber.size()>0){
  return queueNumber.remove(0);
}
return number;
}
}


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

public class Constans {
public static int MAX_SERVICE_TIME=10000;
public static int MIX_SERVICE_TIME=1000;
public static int COMON_CUSTOMER_INTERVAL_TIME=1;

}


public class ServiceWindow {
private CustomerType type = CustomerType.COMMON;
private int windowId = 1;
public CustomerType getType() {
  return type;
}
public void setType(CustomerType type) {
  this.type = type;
}
public int getWindowId() {
  return windowId;
}
// 只需要一个线程
public void setWindowId(int windowId) {
  this.windowId = windowId;
}
public void start() {
  Executors.newSingleThreadExecutor().execute(new Runnable() {
   @Override
   public void run() {
    // 不停地取号
    while (true) {
     switch (type) {
     case COMMON:
      comnonService();
      break;
     case EXPRESS:
      expressService();
      break;
     case VIP:
      vipService();
      break;
     }
    }
   }
  });
}

private void comnonService() {
  String windowName = "第" + windowId + "号" + type + "窗口";
  Integer number = NumberMachine.getInstance().getCommonManager()
    .fctchServiceNumber();
  System.out.println(windowName + "正在获取任务");
  if (number != null) {
   System.out
     .println(windowName + "为第" + number + "个"+"普通客户服务");
   long beginTime = System.currentTimeMillis();
   int maxRand = Constans.MAX_SERVICE_TIME - Constans.MIX_SERVICE_TIME;
   long serverTime = new Random().nextInt(maxRand) + 1
     + Constans.MIX_SERVICE_TIME;
   try {
    Thread.sleep(serverTime);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   long codeTime = System.currentTimeMillis() - beginTime;
   System.out.println(windowName + "为第" + number + "个"
     + "普通客户服务完成服务,耗时" + codeTime / 1000 + "秒");
  } else {
   System.out.println(windowName + "没有获取任务,先休息一下秒嘛!");
   try {
    Thread.sleep(1000);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
}
private void expressService() {
  String windowName = "第" + windowId + "号" + type + "窗口";
  Integer number = NumberMachine.getInstance().getExpressManager()
    .fctchServiceNumber();
  System.out.println(windowName + "正在获取快速任务");
  if (number != null) {
   System.out.println(windowName + "为第" + number + "个" + type + "客户服务");
   long beginTime = System.currentTimeMillis();
   // int maxRand=Constans.MAX_SERVICE_TIME-Constans.MIX_SERVICE_TIME;
   // long serverTime=new
   // Random().nextInt(maxRand)+1+Constans.MIX_SERVICE_TIME;
   try {
    Thread.sleep(Constans.MIX_SERVICE_TIME);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   long codeTime = System.currentTimeMillis() - beginTime;
   System.out.println(windowName + "为第" + number + "个" + type
     + "客户服务完成服务,耗时" + codeTime / 1000 + "秒");
  } else {
   System.out.println(windowName + "正在获取任务!");
   comnonService();
   
  }
  }

private void vipService() {
  String windowName = "第" + windowId + "号" + type + "窗口";
  Integer number = NumberMachine.getInstance().getVipManager()
    .fctchServiceNumber();
  System.out.println(windowName + "正在获取任务");
  if (number != null) {
   System.out
     .println(windowName + "为第" + number + "个" + type + "客户服务");
   long beginTime = System.currentTimeMillis();
   int maxRand = Constans.MAX_SERVICE_TIME - Constans.MIX_SERVICE_TIME;
   long serverTime = new Random().nextInt(maxRand) + 1
     + Constans.MIX_SERVICE_TIME;
   try {
    Thread.sleep(serverTime);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   long codeTime = System.currentTimeMillis() - beginTime;
   System.out.println(windowName + "为第" + number + "个" + type
     + "客户服务完成服务,耗时" + codeTime / 1000 + "秒");
  } else {
   System.out.println(windowName + "没有取到任务");
   comnonService();
  }
}

测试类:
public class MainClass {

public static void main(String args[]){
  
  for(int i=1;i<5;i++){
   ServiceWindow  commWindow=new ServiceWindow();
   commWindow.setWindowId(i);
   commWindow.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().generateManager();
      System.out.println(number+"号普通客户等待服务");
     }
    },
    0,
    Constans.COMON_CUSTOMER_INTERVAL_TIME,
    TimeUnit.SECONDS
    );
  
  //
  
  Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
    new Runnable() {
     public void run() {
      Integer number=NumberMachine.getInstance().getExpressManager().generateManager();
      System.out.println(number+"号快速 客户等待服务");
     }
    },
    0,
    Constans.COMON_CUSTOMER_INTERVAL_TIME*6,
    TimeUnit.SECONDS
    );
  
  //
  Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
    new Runnable() {
     public void run() {
      Integer number=NumberMachine.getInstance().getVipManager().generateManager();
      System.out.println(number+"号Vip客户等待服务");
      
     }
    },
    0,
    Constans.COMON_CUSTOMER_INTERVAL_TIME*2,
    TimeUnit.SECONDS
    );
}
}




2 个回复

倒序浏览
这个问题确实不容易发现,你应该是在细节上有了差错~
回复 使用道具 举报
问题已经解决了,谢谢你
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马