新手发帖,不太懂规矩,有错误的地方请大家见谅,欢迎批评指正
package com.qdzks;
/*目题:
拟模实现银行务业调度统系逻辑,具体求需如下:
? 银行内有6个务业口窗,1 - 4号口窗为一般口窗,5号口窗为倏地口窗,6号口窗为VIP口窗。
? 有三种对应类型的客户:VIP客户,一般客户,倏地客户(理办如交水电费、电话费之类务业的客户)。
? 步异随机成生各种类型的客户,成生各类型户用的率概比例为:
VIP客户 :一般客户 :倏地客户 = 1 :6 :3。
? 客户理办务业所需间时有最大值和小最值,在该范围内随机设定个每VIP客户以及一般客户理办务业所需的间时,倏地客户理办务业所需间时为小最值(提示:理办务业的进程可通过线程Sleep的方法拟模)。
? 各类型客户在其对应口窗按序顺顺次理办务业。
? 当VIP(6号)口窗和倏地务业(5号)口窗没有客户等待理办务业的时候,这两个口窗可以理处一般客户的务业,而一旦有对应的客户等待理办务业的时候,则优先理处对应客户的务业。
? 随机成生客户间时间隔以及务业理办间时最大值和小最值自定,可以设置。
? 不要求实现GUI,只虑考统系逻辑实现,可通过Log方法展示程序运行结果。
须要用到的知识析剖:举枚、线程池、好良的面向对象编程
具体的知识点,我会在代码用到的时候解讲
面向对象的分析与计设:
有三种对应类型的客户:VIP客户,一般客户,倏地客户 ,步异随机成生各种类型的客户,各类型客户在其对应口窗按序顺顺次理办务业 。
首先,经常在银行理办务业的人更有利于解理本统系,例如,我经常陪婆老跑银行,对银行的这个务业算是比拟悉熟了,我道知每个客户其实就是由银行的一个取号器机发生号码的方法来示表的。所以,我想到要有一个号码管理器对象,让这个对象不断地发生号码,就于等随机成生了客户。
由于有三类客户,每类客户的号码编排都是完整独立的,所以,我想到本统系一共要发生三个号码管理器对象,各自管理一类户用的排队号码。这三个号码管理器对象一统由一个号码器机停止管理,这个号码器机在个整统系中一直只能有一个,所以,它要被计设成单例。
各类型客户在其对应口窗按序顺顺次理办务业 ,精确地说,应该是口窗顺次叫号。
各个口窗怎么道知该叫个一哪号了呢?它一定是问的响应的号码管理器,即服务口窗每次找号码管理器取获前当要被服务的号码。
NumberManager和NumberMachine类
lNumberManager类
定义一个用于存储上一个客户号码的员成量变和用于存储全部等待服务的客户号码的列队集合。
定义一个发生新号码的方法和取获马上要为之服务的号码的方法,这两个方法被不同的线程操作了雷同的数据,所以,要停止同步。
lNumberMachine类
定义三个员成量变分离向指三个NumberManager对象,分离示表一般、倏地和VIP客户的号码管理器,定义三个对应的方法来返回这三个NumberManager对象。
将NumberMachine类计设成单例。
ServiceWindow与CustomerType举枚类
lCustomerType举枚类
统系中有三种类型的客户,所以用定义一个举枚类,其中定义三个员成分离示表三种类型的客户。
重 写toString方法,返回类型的中文名称。这是在后面编码时重构出来的,刚开始用不虑考。
lServiceWindow类
定义一个start方法,外部启动一个线程,根据服务口窗的别类分离循环用调三个不同的方法。
定义三个方法分离对三种客户停止服务,为了视察运行效果,应具体打印出其中的细节信息。
MainClass类与Constants类
lMainClass类
用for循环建创出4个一般口窗,再建创出1个倏地口窗和一个VIP口窗。
接着再建创三个准时器,分离准时去建创新的一般客户号码、新的倏地客户号码、新的VIP客户号码。
lConstants类
定义三个量常:MAX_SERVICE_TIME、MIN_SERVICE_TIME、COMMON_CUSTOMER_INTERVAL_TIME
思惟理楚清后之,我们就开始写代码了=========================================*/
//NumberManager类
import java.util.ArrayList;
import java.util.List;
class NumberManager {
//建创一个集合,用于存储取号的客户职员
private int lastNumber=1;
private List<Integer> queueNumber=new ArrayList<Integer>();
public synchronized Integer generateNewManager(){//这个方法用于成生客户,并把客户存于集合中
queueNumber.add(lastNumber);
return lastNumber++;
}
public synchronized Integer fetchServiceNumber(){//这个方法用于取号,当服务口窗停止对客户的服务时,通过这个方法取号
Integer number=null;
if(queueNumber.size()>0)//且记一定要判断集合是不是有元素,否则会出现异常
number=queueNumber.remove(0);
return number;
}
}
//NumberMachine类重要用于取获发生客户的类型 计设成单例模式
public class NumberMachine {
private NumberManager commomManager = new NumberManager();
public NumberManager getCommomManager() {
return commomManager;
}
public NumberManager getExpressManager() {
return expressManager;
public NumberManager getVipManager() {
return vipManager;
}
private NumberManager expressManager =new NumberManager();
private NumberManager vipManager=new NumberManager();
计设单例模式饿汉式住记三步
1.将构造函数私有化
2.new 一个私有的本类本类的对象
3.通过静态方法,返回本类对象
//将构造函数私有化
private NumberMachine(){
}
//一个静态私有的本类对象
private static NumberMachine machine=new NumberMachine();
//供给一个方法返回本类对象
public static NumberMachine getInstance(){
return machine;
}
}
定义一个举枚类:举枚类对象为客户的类型
public enum CustomerType {
COMMON,EXPRESS,VIP;//客户类型为一般客户,倏地客户,VIP客户
public String toString(){//写重toString 方法
switch(this){//注:switch 里头可以有整数类型的量变 和举枚类型的量变
case COMMON: return "一般";
case EXPRESS: return "倏地";
case VIP: return name();
}
return null;
}
}
Contanis 类,重要量变为存储服务的最大间时以及小最间时
public class Contants {
public static int MAX_SERVICE_TIME=10000;
public static int MIN_SERVICE_TIME=1000;
public static int COMMON_CUSTOMER_INTERVAL_TIME=1;
}
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
ServiceWindow口窗的编写//在这用到了线程池的解讲
线程池的念概与Executors类的用应
首先分析在TCP服务器编程模型的理原,当一个客户端连接到服务器,服务器要起一个线程为之服,当客户端的回话束结时,线程也就束结了,即每个客户端连接,服务器 就要为之建创一个新的线程,这比好设假个每报名生学都要通过我来亲自接 待,以便给个每生学一种好的觉感,但个每生学报名手续消费半个小时,对于 50名生学,我一个个待接和为之办手续,显然是不实际的,那么我会怎么做呢
?我会先待接个每生学,打完召唤后,再把他分配给一位工作职员去理办手续 ,这样我就待接了每一位生学.
如果问访服务器的客户端很多,那么服务器要不断的建创和销毁线程, 这样将严重影响服务器的性能,如果真的来一位生学,我们就布置一位新的 工作职员为之服务,这也是不可能的,那么公司岂不是要招很多工作职员.
应该是一位工作职员服务完一位生学,闲空来下后,一旦有新的生学要服务,
我们布置该工作职员,为之服务.
线程池的念概于此相似,首先建创一些线程,他们的集合称为线程池,
当服务器接收到一个客户请求后,就从线程池中出取一个闲空的线程为之服
务,服务实现后不关闭该线程,而是将该线程还回到线程池中.
在线程池的编程模式下,务任是交给个整线程池,而不是直接交给某个线
程,线程池拿到务任偶,他就在外部找有闲空的线程,再把务任交给外部某个
闲空的线程,这就是封装.住记,务任是交给个整线程池,但可以同时向一个线程池中提交多个务任.
建创牢固小大的线程池:
ExecutorService threadPool = Executors.newFixedThreadPool(3);
threadPool.execute(Runnable runnable);
建创存缓的线程池:
如果线程够不主动建创新的线程足满服务
ExecutorService threadPool = Executors.newCachedThreadPool();
threadPool.execute(Runnable runnable);
建创单一线程池.
线程里头只有一个线程,如果该线程不测亡死,那么统系会主动建创一个新的线程来取代.
ExecutorService threadPool = Executors.newSingleThreadExecutor();
threadPool.execute(Runnable runnable);
//执行完务任后关闭线程池,
threadPool.shutdown();
//不管务任是不是实现都关闭线程池
threadPool.shutdownNow();
用线程池启动准时器.
Executors.newScheduledThreadPool(3).schedule(new Runnable(){
@Override
publicvoid run() {
}
}, 20, TimeUnit.SECONDS);
public class ServiceWindow {
private CustomerType type=CustomerType.COMMON;//认默的客户类型为一般客户
private int windowId=1;//认默的客户口窗号
public void setType(CustomerType type){//对外供给设置口窗类型的方法和口窗编号的方法
this.type=type;
}
public void setWidowId(int windowId){
this.windowId=windowId;
}
public void run(){
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
public void run(){
while(true){
switch(type){
case COMMON: commonService();
break;
case EXPRESS: expressService();break;
case VIP : vipService();break;
}
}
}
});
}
public void commonService(){
String windowName="第"+windowId+"号"+type+"口窗";
Integer number=NumberMachine.getInstance().getCommomManager().fetchServiceNumber();
System.out.println(windowName+"正在取获务任");
if(number!=null){
System.out.println(windowName+"正在为第"+number+"个"+"一般客户服务");
long beginTime=System.currentTimeMillis();
int maxRand=Contants.MAX_SERVICE_TIME-Contants.MIN_SERVICE_TIME;
long serveTime = new Random().nextInt(maxRand)+1+Contants.MIN_SERVICE_TIME;
try {
Thread.sleep(serveTime);
} catch (Exception e) {
// TODO: handle exception
}
long endTime=System.currentTimeMillis();
long costTime=endTime-beginTime;
System.out.println(windowName+"为第"+number+"个"+"一般"+"客户实现服务,耗时"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到务任,苏息一秒");
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
public void expressService(){
String windowName="第"+windowId+"号"+type+"口窗";
Integer number=NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
System.out.println(windowName+"正在取获务任");
if(number!=null){
System.out.println(windowName+"正在为第"+number+"个"+type+"客户服务");
long beginTime=System.currentTimeMillis();
int maxRand=Contants.MAX_SERVICE_TIME-Contants.MIN_SERVICE_TIME;
long serveTime = new Random().nextInt(maxRand)+1+Contants.MIN_SERVICE_TIME;
try {
Thread.sleep(serveTime);
} catch (Exception e) {
// TODO: handle exception
}
long endTime=System.currentTimeMillis();
long costTime=endTime-beginTime;
System.out.println(windowName+"为第"+number+"个"+type+"客户实现服务,耗时"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到服务务任");
commonService();
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
public void vipService(){
String windowName="第"+windowId+"号"+type+"口窗";
Integer number=NumberMachine.getInstance().getVipManager().fetchServiceNumber();
System.out.println(windowName+"正在取获务任");
if(number!=null){
System.out.println(windowName+"正在为第"+number+"个"+type+"客户服务");
long beginTime=System.currentTimeMillis();
int maxRand=Contants.MAX_SERVICE_TIME-Contants.MIN_SERVICE_TIME;
long serveTime = new Random().nextInt(maxRand)+1+Contants.MIN_SERVICE_TIME;
try {
Thread.sleep(serveTime);
} catch (Exception e) {
// TODO: handle exception
}
long endTime=System.currentTimeMillis();
long costTime=endTime-beginTime;
System.out.println(windowName+"为第"+number+"个"+type+"客户实现服务,耗时"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到务任");
commonService();
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
//MainClass编写
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<5;i++){
ServiceWindow commonWindow=new ServiceWindow();
commonWindow.setType(CustomerType.COMMON);
commonWindow.setWidowId(i);
commonWindow.run();
}
ServiceWindow expressWindow=new ServiceWindow();
expressWindow.setType(CustomerType.EXPRESS);
expressWindow.setWidowId(5);
expressWindow.run();
ServiceWindow vipWindow=new ServiceWindow();
vipWindow.setType(CustomerType.VIP);
vipWindow.setWidowId(6);
vipWindow.run();
ScheduledExecutorService timer1=Executors.newScheduledThreadPool(1);
timer1.scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number=NumberMachine.getInstance().getCommomManager().generateNewManager();
System.out.println(number+"一般客户等待服务");
}
},
0,
Contants.COMMON_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
timer1.scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number=NumberMachine.getInstance().getExpressManager().generateNewManager();
System.out.println(number+"倏地客户等待服务");
}
},
0,
Contants.COMMON_CUSTOMER_INTERVAL_TIME*2,
TimeUnit.SECONDS);
timer1.scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number=NumberMachine.getInstance().getVipManager().generateNewManager();
System.out.println(number+"VIP客户等待服务");
}
},
0,
Contants.COMMON_CUSTOMER_INTERVAL_TIME*6,
TimeUnit.SECONDS);
}
}
文章结束给大家分享下程序员的一些笑话语录: 人工智能今天的发展水平:8乘8的国际象棋盘其实是一个体现思维与创意的强大媒介。象棋里蕴含了天文数字般的变化。卡斯帕罗夫指出,国际象棋的合法棋步共有1040。在棋局里每算度八步棋,里面蕴含的变化就已经超过银河系里的繁星总数。而地球上很少有任何数量达到这个级别。在金融危机之前,全世界的财富总和大约是1014人民币,而地球人口只有1010。棋盘上,所有可能的棋局总数达到10120,这超过了宇宙里所有原子的总数!经典语录网