本帖最后由 zczcyc0201 于 2012-10-9 22:01 编辑
看了张老师的教程,理解了思路,自己从头到尾写了一遍。和张老师的有些不一样。
通过写这个还有交通灯那个例子的代码,总结了几条新手容易犯的错误,至少我是犯了,找了半天才找出来。
我写代码的过程中犯的错误有:
1.用一个指向null的对象调用某个方法,可能会导致程序停住,而且没有任何提示。
2.如果想验证一个集合或数组是否为空,不要用if(ArrayList=!null)这样的条件语句,要用ArrayList.size()>0这种条件语句。
3.所有while(true)的代码,不要忘了用单独的线程运行,否则程序会停住。
下面是代码:
package com.zcz.bank;
import java.util.*;
//号码管理器类
public class NumberManager {
//定义一个集合存放顾客
private List<Integer> consumers = new ArrayList<Integer>();
private Integer i = 0;
//生成顾客的方法
public synchronized Integer generateNumber ()
{
consumers.add(++i);
//顾客顺序编号增加
return i;
}
//窗口向号码管理器取号的方法
public synchronized Integer giveNumber()
{
if(consumers.size()>0)
{
int removed = consumers.remove(0);
return removed;
}
else{
return null;
}
}
}
//取号机类 单例模式
package com.zcz.bank;
public class NumberMachine {
private NumberManager commonNumberManager = new NumberManager();
private NumberManager quickNumberManager = new NumberManager();
private NumberManager VIPNumberManager = new NumberManager();
private NumberMachine(){
}
private static NumberMachine instance = new NumberMachine();
public static NumberMachine getInstance(){
return instance;
}
public NumberManager getCommonNumberManager()
{
return commonNumberManager;
}
public NumberManager getQuickNumberManager()
{
return quickNumberManager;
}
public NumberManager getVIPNumberManager()
{
return VIPNumberManager;
}
}
//服务窗口类
package com.zcz.bank;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ServeWindow {
private ConsumersType type;
private Integer windowID;
//让窗口初始化就具备类型和窗口号
ServeWindow(ConsumersType type,Integer windowID)
{
this.type=type;
this.windowID=windowID;
}
//窗口叫号方法
public void fetchNumber()
{
Executors.newSingleThreadExecutor().execute(new Runnable(){
public void run(){
while(true)
{
switch(type)
{
case COMMON:
commonServe();
break;
case QUICK:
quickServe();
break;
case VIP:
VipServe();
break;
}
}
}
});
}
public void commonServe()
{
System.out.println("第"+windowID+"号"+type+"窗口"+"开始获取普通任务");
Integer ServeNumber = NumberMachine.getInstance().getCommonNumberManager().giveNumber();
if(ServeNumber != null)
{
System.out.println("第"+windowID+"号"+type+"窗口开始为"+"第"+ServeNumber+"普通顾客服务");
long startTime= System.currentTimeMillis();
try {
Thread.sleep(new Random().nextInt(Constants.DEAL_TASK_MAX_TIME-Constants.DEAL_TASK_MIN_TIME+1)+Constants.DEAL_TASK_MIN_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis()-startTime;
System.out.println("第"+windowID+"号"+type+"窗口为"+"第"+ServeNumber+"号普通顾客服务服务完毕,耗时:"+costTime/1000+"秒!");
}else{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("暂时没有普通顾客等待");
}
}
public void quickServe()
{
System.out.println("第"+windowID+"号"+type+"窗口"+"开始获取快速任务");
Integer ServeNumber = NumberMachine.getInstance().getQuickNumberManager().giveNumber();
if(ServeNumber != null)
{
System.out.println("第"+windowID+"号"+type+"窗口开始为"+"第"+ServeNumber+"快速顾客服务");
long startTime= System.currentTimeMillis();
try {
Thread.sleep(Constants.DEAL_TASK_MIN_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis()-startTime;
System.out.println("第"+windowID+"号"+type+"窗口为"+"第"+ServeNumber+"号快速顾客服务服务完毕,耗时:"+costTime/1000+"秒!");
}else{
System.out.println("暂时没有快速顾客等待");
commonServe();
}
}
public void VipServe()
{
System.out.println("第"+windowID+"号"+type+"窗口"+"开始获取VIP任务");
Integer ServeNumber = NumberMachine.getInstance().getVIPNumberManager().giveNumber();
if(ServeNumber != null)
{
System.out.println("第"+windowID+"号"+type+"窗口为"+"第"+ServeNumber+"普通顾客服务");
long startTime= System.currentTimeMillis();
try {
Thread.sleep(new Random().nextInt(Constants.DEAL_TASK_MAX_TIME-Constants.DEAL_TASK_MIN_TIME+1)+Constants.DEAL_TASK_MIN_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis()-startTime;
System.out.println("第"+windowID+"号"+type+"窗口为"+"第"+ServeNumber+"号VIP顾客服务服务完毕,耗时:"+costTime/1000+"秒!");
}else{
System.out.println("暂时没有VIP顾客等待");
commonServe();
}
}
}
//常量保存类
package com.zcz.bank;
public class Constants {
public static final int GENERATE_CONSUMER_MIN_TIME = 1000;
public static final int GENERATE_CONSUMER_MAX_TIME = 2000;
public static final int DEAL_TASK_MIN_TIME = 1000;
public static final int DEAL_TASK_MAX_TIME = 5000;
}
//客户类型枚举类
package com.zcz.bank;
public enum ConsumersType {
COMMON,QUICK,VIP;
}
//main方法类
package com.zcz.bank;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
new ServeWindow(ConsumersType.COMMON,1).fetchNumber();
new ServeWindow(ConsumersType.COMMON,2).fetchNumber();
new ServeWindow(ConsumersType.COMMON,3).fetchNumber();
new ServeWindow(ConsumersType.COMMON,4).fetchNumber();
new ServeWindow(ConsumersType.QUICK,5).fetchNumber();
new ServeWindow(ConsumersType.VIP,6).fetchNumber();
//普通客户拿号
Executors.newSingleThreadExecutor().execute(new Runnable(){
public void run()
{while(true)
{
Integer serviceNumber = NumberMachine.getInstance().getCommonNumberManager().generateNumber();
System.out.println("第" + serviceNumber + "号普通客户正在等待服务!");
try {
Thread.sleep(Constants.GENERATE_CONSUMER_MIN_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}}
});
//快速客户拿号
Executors.newSingleThreadExecutor().execute(new Runnable(){
public void run()
{
while(true)
{
Integer serviceNumber = NumberMachine.getInstance().getQuickNumberManager().generateNumber();
System.out.println("第" + serviceNumber + "号快速客户正在等待服务!");
try {
Thread.sleep(2*Constants.GENERATE_CONSUMER_MIN_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}}
});
//VIP客户拿号
Executors.newSingleThreadExecutor().execute(new Runnable(){
public void run()
{
while(true)
{
Integer serviceNumber = NumberMachine.getInstance().getVIPNumberManager().generateNumber();
System.out.println("第" + serviceNumber + "号VIP客户正在等待服务!");
try {
Thread.sleep(6*Constants.GENERATE_CONSUMER_MIN_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}}
});
}}
|