黑马程序员--7k面试题:银行业务调度系统(和老师写的不一样哟)

来源:互联网 发布:游戏力 知乎 编辑:程序博客网 时间:2024/05/17 00:51

----------------------ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

package adv02;/** * 这是一个号码管理器,用单例,因为只有一个机器。 * 里面产生各类客户,说白了就是客户取号不就是产生客户吗? * 它还提供方法可以取走最前面的一个客户。。 */import java.util.ArrayList;import java.util.List;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class NumberMachine{private int commonnumber;private int expressnumber;private int vipnumber;private List<String> commonqueue = new ArrayList<String>();private List<String> expressqueue = new ArrayList<String>();private List<String> vipqueue = new ArrayList<String>();private static NumberMachine instance = new NumberMachine();private NumberMachine(){ScheduledExecutorService producecommon = Executors.newSingleThreadScheduledExecutor();producecommon.scheduleAtFixedRate(new Runnable(){public void run(){commonqueue.add(++commonnumber+"号普通客户");//这就是客户取号。取一个号就相当于给集合加一个元素。System.out.println(commonnumber+"号普通客户"+"正在等待服务####");}}, 1,1, //用随机数,或者别的更好。TimeUnit.SECONDS);ScheduledExecutorService expresscommon = Executors.newSingleThreadScheduledExecutor();expresscommon.scheduleAtFixedRate(new Runnable(){public void run(){expressqueue.add(++expressnumber+"号快速客户");//这就是客户取号。取一个号就相当于给集合加一个元素。System.out.println(expressnumber+"号快速客户"+"正在等待服务####");}}, 3,3, //用随机数,或者别的更好。TimeUnit.SECONDS);ScheduledExecutorService vipcommon = Executors.newSingleThreadScheduledExecutor();vipcommon.scheduleAtFixedRate(new Runnable(){public void run(){vipqueue.add(++vipnumber+"号VIP客户");//这就是客户取号。取一个号就相当于给集合加一个元素。System.out.println(vipnumber+"号VIP客户"+"正在等待服务####");}}, 6,6, //用随机数,或者别的更好。TimeUnit.SECONDS);}public static NumberMachine getInstance(){return instance;}public synchronized String fetchcommon()//取普通客户。{if(commonqueue.size()>0){return commonqueue.remove(0);}elsereturn null;}public synchronized String fetchexpress()//取快还客户{if(expressqueue.size()>0){return expressqueue.remove(0);}elsereturn null;}public synchronized String fetchvip()//取vip客户。{if(vipqueue.size()>0){return vipqueue.remove(0);}elsereturn null;}}

package adv02;import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CommonWindow{private int windownumber;public CommonWindow(int windownumber){super();this.windownumber = windownumber;System.out.println(windownumber+"号普通窗口启动");}public void start(){ExecutorService commonthread = Executors.newSingleThreadExecutor();commonthread.execute(new Runnable(){public void run(){while(true){NumberMachine numbermachine = NumberMachine.getInstance();String common = numbermachine.fetchcommon();//得到取号器普通客户的集合中的最前面的一个。if(common!=null){System.out.println(windownumber+"号普通窗口正在服务----"+common);try{int time = new Random().nextInt(10)+1;Thread.sleep(time*1000);//休息一下,就相当于服务时间。System.out.println(windownumber+"号普通窗口服务完"+common+"用时"+time);} catch (InterruptedException e){// TODO 自动生成的 catch 块e.printStackTrace();}}else{System.out.println(windownumber+"号普通窗口没有客户,休息一秒。");try{Thread.sleep(1000);} catch (InterruptedException e){// TODO 自动生成的 catch 块e.printStackTrace();}}}}});}}

package adv02;import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ExpressWindow{private int windownumber;public ExpressWindow(int windownumber){super();this.windownumber = windownumber;System.out.println(windownumber+"号快速窗口启动");}public void start(){ExecutorService expressthread = Executors.newSingleThreadExecutor();expressthread.execute(new Runnable(){public void run(){while(true){NumberMachine numbermachine = NumberMachine.getInstance();String express = numbermachine.fetchexpress();if(express!=null){System.out.println(windownumber+"号快速窗口正在服务----"+express);try{int time = new Random().nextInt(3)+1;Thread.sleep(time*1000);System.out.println(windownumber+"号快速窗口服务完"+express+"用时"+time+"!!!");} catch (InterruptedException e){// TODO 自动生成的 catch 块e.printStackTrace();}}else{String common = numbermachine.fetchcommon();if(common!=null){System.out.println(windownumber+"号快速窗口正在服务----"+common);try{int time = new Random().nextInt(6)+1;Thread.sleep(time*1000);System.out.println(windownumber+"号快速窗口服务完"+common+"用时"+time+"!!!");} catch (InterruptedException e){// TODO 自动生成的 catch 块e.printStackTrace();}}else{System.out.println(windownumber+"号快速窗口没有客户,休息一秒。");try{Thread.sleep(1000);} catch (InterruptedException e){// TODO 自动生成的 catch 块e.printStackTrace();}}}}}});}}

package adv02;import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class VIPWindow{private int windownumber;public VIPWindow(int windownumber){super();this.windownumber = windownumber;System.out.println(windownumber+"号VIP窗口启动");}public void start(){ExecutorService vipthread = Executors.newSingleThreadExecutor();vipthread.execute(new Runnable(){public void run(){while(true){NumberMachine numbermachine = NumberMachine.getInstance();String vip = numbermachine.fetchvip();if(vip!=null){System.out.println(windownumber+"号VIP窗口正在服务----"+vip);try{int time = new Random().nextInt(9)+1;Thread.sleep(time*1000);System.out.println(windownumber+"号VIP窗口服务完"+vip+"用时"+time+"!!!");} catch (InterruptedException e){// TODO 自动生成的 catch 块e.printStackTrace();}}else{String common = numbermachine.fetchcommon();if(common!=null){System.out.println(windownumber+"号VIP窗口正在服务----"+common);try{int time = new Random().nextInt(6)+1;Thread.sleep(time*1000);System.out.println(windownumber+"号VIP窗口服务完"+common+"用时"+time+"!!!");} catch (InterruptedException e){// TODO 自动生成的 catch 块e.printStackTrace();}}else{System.out.println(windownumber+"号VIP窗口没有客户,休息一秒。");try{Thread.sleep(1000);} catch (InterruptedException e){// TODO 自动生成的 catch 块e.printStackTrace();}}}}}});}}

package adv02;/** * 思路:1,用一个号码机器产生各类客户,给各类客户编号,并用集合存起来。 * 并对外提供一个方法,能得到最前面的客户。这个取号器只有一个。所以用单例。 * 2,做三种窗口。每一个窗口都向机器要客户。根据返回值来判断有没有客户。 * 感觉很简单。。 * @author wangyang * */public class MainClass{public static void main(String[] args){NumberMachine.getInstance();for(int x=1;x<=3;x++){new CommonWindow(x).start();}new ExpressWindow(1).start();new VIPWindow(1).start();}}


----------------------ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

0 0
原创粉丝点击