对象分析:
1.客户类按照业务分类,客户可以分成三类。
public enum Custommer {
COMMON ,VIP , EXPRESS;
public String
toString(){
String name =null ;
switch (this ){
case COMMON :
name ="普通" ;
break;
case VIP :
name ="VIP" ;
break;
case EXPRESS :
name ="快速" ;
break;
}
return name;
}
}
2.号码机类。号码机用于产生各种号码管理类。
public class NumberMachine {
private NumberMachine(){}
private static NumberMachine instance= new NumberMachine() ;
public static NumberMachine
getInstance (){
return instance ;
}
private NumberManager commonManager = new NumberManager ();
private NumberManager expressManager = new NumberManager ();
private NumberManager vipManager = new NumberManager ();
public NumberManager
getCommonManager() {
return commonManager ;
}
public NumberManager
getExpressManager() {
return expressManager ;
}
public NumberManager
getVipManager() {
return vipManager ;
}
}
3.号码管理类。号码机管理类主要作用存放号码,给外界提供号码信息。
import java. util.*;
public class NumberManager {
private int lastnumber= 0;
private List queueNumber= new ArrayList ();
public synchronized Integer generateNewNumber (){
queueNumber .add ( lastnumber++ );
return lastnumber ;
}
public synchronized Integer fetchNumber (){
if (queueNumber .size ()> 0 )
return ( Integer ) queueNumber. remove (0 );
else return null;
}
}
4.服务窗口类。为客户提供服务
public class ServiceWindow {
Custommer type= Custommer. COMMON;
public Custommer
getType() {
return type ;
}
public void setType (Custommer
type ) {
this. type = type;
}
public int getNumber () {
return number ;
}
public void setNumber (int number ) {
this. number = number;
}
int number =1 ;
public void start (){
Executors .newSingleThreadExecutor () .execute ( new Runnable (){
public void run () {
// TODO Auto-generated method
stub
while ( true )
switch ( type ){
case COMMON :
commonService ();
break;
case VIP :
vipService ();
break;
case EXPRESS :
expressService ();
}
}
}) ;
}
private void commonService () {
// TODO Auto-generated method
stub
String
windowName = "第" + number + "号" + type + "窗口" ;
System . out. println( windowName + "开始获取普通任务!" );
Integer
serviceNumber = NumberMachine.getInstance (). getCommonManager (). fetchNumber() ;
if (serviceNumber != null ){
System . out. println( windowName + "开始为第" + serviceNumber + "号普通客户服务" );
int maxRandom = Constants. MAX_SERVICE_TIME - Constants. MIN_SERVICE_TIME;
int serviceTime = new Random (). nextInt( maxRandom )+ 1 + Constants. MIN_SERVICE_TIME;
try {
Thread .sleep (serviceTime );
} catch (InterruptedException e ) {
e .printStackTrace ();
}
System . out. println( windowName + "完成为第" + serviceNumber + "号普通客户服务,总共耗时" +serviceTime/ 1000 + "秒") ;
} else{
System . out. println( windowName + "没有取到普通任务,正在空闲一秒" );
try {
Thread .sleep (1000 );
} catch (InterruptedException e ) {
e .printStackTrace ();
}
}
}
private void vipService () {
// TODO Auto-generated method
stub
Integer
serviceNumber = NumberMachine.getInstance (). getExpressManager (). fetchNumber() ;
String
windowName = "第" + number + "号" + type + "窗口" ;
System . out. println( windowName + "开始获取快速任务!" );
if (serviceNumber != null){
System . out. println( windowName + "开始为第" + serviceNumber + "号快速客户服务" );
int serviceTime = Constants. MIN_SERVICE_TIME;
try {
Thread .sleep (serviceTime );
} catch (InterruptedException e ) {
e .printStackTrace ();
}
System . out. println( windowName + "完成为第" + serviceNumber + "号快速客户服务,总共耗时" +serviceTime/ 1000 + "秒") ;
} else{
System . out. println( windowName + "没有取到快速任务!" );
commonService ();
}
}
private void expressService () {
// TODO Auto-generated method
stub
Integer
serviceNumber = NumberMachine.getInstance (). getVipManager (). fetchNumber() ;
String
windowName = "第" + number + "号" + type + "窗口" ;
System . out. println( windowName + "开始获取VIP任务!" );
if (serviceNumber != null){
System . out. println( windowName + "开始为第" + serviceNumber + "号VIP客户服务" );
int maxRandom = Constants. MAX_SERVICE_TIME - Constants. MIN_SERVICE_TIME;
int serviceTime = new Random (). nextInt( maxRandom )+ 1 + Constants. MIN_SERVICE_TIME;
try {
Thread .sleep (serviceTime );
} catch (InterruptedException e ) {
e .printStackTrace ();
}
System . out. println( windowName + "完成为第" + serviceNumber + "号VIP客户服务,总共耗时" +serviceTime/ 1000 + "秒") ;
} else{
System . out. println( windowName + "没有取到VIP任务!" );
commonService ();
}
}
}
5.测试类类。
class Constants {
public final static int MAX_SERVICE_TIME =10 ;
public final static int MIN_SERVICE_TIME =1 ;
public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;
}
public class MainClass {
private static Logger logger = Logger .getLogger ("threadTest" );
public static void main ( String[] args ) {
//产生4个普通窗口
for (int i= 1; i< 5; i++ ){
ServiceWindow
window = new ServiceWindow() ;
window .setNumber ( i) ;
window .start ();
}
//产生1个快速窗口
ServiceWindow
expressWindow = new ServiceWindow() ;
expressWindow .setType ( Custommer. EXPRESS) ;
expressWindow .start ();
//产生1个VIP窗口
ServiceWindow
vipWindow = new ServiceWindow() ;
vipWindow .setType ( Custommer. VIP) ;
vipWindow .start ();
//普通客户拿号
Executors .newScheduledThreadPool ( 1) .scheduleAtFixedRate (
new Runnable(){
public void run (){
Integer
serviceNumber = NumberMachine.getInstance (). getCommonManager (). generateNewNumber() ;
System . out. println( "第" + serviceNumber + "号普通客户正在等待服务!" );
}
},
0 ,
Constants . COMMON_CUSTOMER_INTERVAL_TIME,
TimeUnit . SECONDS) ;
//快速客户拿号
Executors .newScheduledThreadPool ( 1) .scheduleAtFixedRate (
new Runnable(){
public void run (){
Integer
serviceNumber = NumberMachine.getInstance (). getExpressManager (). generateNewNumber() ;
System . out. println( "第" + serviceNumber + "号快速客户正在等待服务!" );
}
},
0 ,
Constants . COMMON_CUSTOMER_INTERVAL_TIME * 2 ,
TimeUnit . SECONDS) ;
//VIP客户拿号
Executors .newScheduledThreadPool ( 1) .scheduleAtFixedRate (
new Runnable(){
public void run (){
Integer
serviceNumber = NumberMachine.getInstance (). getVipManager (). generateNewNumber() ;
System . out. println( "第" + serviceNumber + "号VIP客户正在等待服务!" );
}
},
0 ,
Constants . COMMON_CUSTOMER_INTERVAL_TIME * 6 ,
TimeUnit . SECONDS) ;
}
黑马程序员——Java面试题之银行业务调度系统