按照字母的排序发我的代码吧!!
C字母
package bank;
public class Consumer {
public Integer id;
public String type;
public Consumer(Integer id,String type) {
this.id = id;
this.type = type;
}
public void execute(){
try {
if(type.equals("normal")) {
Thread.sleep(1000);
return;
}
if(type.equals("quick")) {
Thread.sleep(300);
return;
}
if(type.equals("vip")) {
Thread.sleep(1000);
return;
}
}catch(Exception E) {
E.printStackTrace();
}
}
}
D字母
package bank;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
public class Dispatcher {
/**
* @param threadPool
* @param Windows
* @param vips
* @param normals
* @param quicks
*/
public void doQuick(ExecutorService threadPool, WindowList Windows,
ArrayBlockingQueue vips,
ArrayBlockingQueue normals, ArrayBlockingQueue quicks) {
try {
while (true) {
// do vip
final Window window = Windows.getIdelQuickWindow();
if (window != null) {
int flag = 0;
final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);
if (id_quick != null)
// quick里面有人
run(window, id_quick, threadPool);
flag = 1;
if (flag == 0) {
final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);
if (id_vip != null)
// quick里面没人 vip队伍里有人排队
run(window, id_vip, threadPool);
flag = 1;
}
if (flag == 0) {
final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);
if (id_normal != null) {
// quick也没有人 vip队伍里没人排队 normal 里面有人
run(window, id_normal, threadPool);
}
}
} else {
// vip窗口正在忙
Thread.sleep(1000);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param id_quick
* @param threadPool
*/
private static void run(final Window window, final Consumer id_quick, ExecutorService threadPool) {
threadPool.execute(new Runnable() {
public void run() {
window.execute(id_quick);
}
});
}
/**
* @param threadPool
* @param list
* @param vips
*/
public void doNomal(ExecutorService threadPool, WindowList list, ArrayBlockingQueue vips,
ArrayBlockingQueue normals, ArrayBlockingQueue quicks) {
try {
while (true) {
// do vip
final Window window = list.getIdelNormalWindow();
if (window != null) {
int flag = 0;
final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);
if (id_normal != null)
// normal 里面有人
run(window, id_normal, threadPool);
flag = 1;
if (flag == 0) {
final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);
if (id_quick != null)
// normal里面没人 quick里面有人
run(window, id_quick, threadPool);
flag = 1;
}
if (flag == 0) {
final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);
if (id_vip != null)
// normal里面没人 normal里面没人 vip队伍里有人排队
run(window, id_vip, threadPool);
flag = 1;
}
} else {
// normal窗口正在忙
Thread.sleep(1000);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param vips
* @param list
* @param threadPool
* @param quicks
* @param normals
*
*/
public void doVIP(ExecutorService threadPool, WindowList list, ArrayBlockingQueue vips,
ArrayBlockingQueue normals, ArrayBlockingQueue quicks) {
try {
while (true) {
// do vip
final Window window = list.getIdelVIPWindow();
if (window != null) {
// vip窗口空闲
// System.out.println("vips is null? "+vips==null);
final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);
int flag = 0;
if (id_vip != null) {
// vip队伍里有人排队
run(window, id_vip, threadPool);
flag = 1;
}
if (flag == 0) {
final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);
if (id_quick != null)
// vip队伍里没人排队 quick里面有人
run(window, id_quick, threadPool);
flag = 1;
}
if (flag == 0) {
final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);
if (id_normal != null)
// vip队伍里没人排队 quick也里面有人 normal 里面有人
run(window, id_normal, threadPool);
}
} else {
// vip窗口正在忙
Thread.sleep(1000);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
M字母
package bank;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class Main {
public static void main(String[] args) {
final ExecutorService threadPool = Executors.newCachedThreadPool();
// 每个队伍 最多有10人
final ArrayBlockingQueue normals = new ArrayBlockingQueue<>(10);
final ArrayBlockingQueue vips = new ArrayBlockingQueue<>(10);
final ArrayBlockingQueue quicks = new ArrayBlockingQueue<>(10);
final WindowList Windows = new WindowList();
final Dispatcher dispatcher=new Dispatcher();
final Producer producer = new Producer();
producer.setNormals(normals);
producer.setQuicks(quicks);
producer.setVips(vips);
threadPool.execute(new Runnable() {
public void run() {
producer.produce();
}
});
threadPool.execute(new Runnable() {
public void run() {
dispatcher.doVIP(threadPool, Windows, vips, normals, quicks);
}
});
threadPool.execute(new Runnable() {
public void run() {
dispatcher.doQuick(threadPool, Windows, vips, normals, quicks);
}
});
threadPool.execute(new Runnable() {
public void run() {
dispatcher.doNomal(threadPool, Windows, vips, normals, quicks);
}
});
threadPool.execute(new Runnable() {
public void run() {
try {
while (true) {
// http://blog.csdn.net/yingzishizhe/article/details/8769907
int threadCount = ((ThreadPoolExecutor) threadPool).getActiveCount();
System.out.println("现在活跃的线程数量为: " + threadCount);
System.out.println("现在排队的人数为:" + (vips.size() + normals.size() + quicks.size()));
Thread.sleep(3000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
P字母
package bank;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
public class Producer {
//省略get/set方法
public ArrayBlockingQueue normals;
public ArrayBlockingQueue vips;
public ArrayBlockingQueue quicks;
public void produce(){
Random random=new Random();
Random time=new Random();
int i=1;
try {
while (true) {
//产生1-10
int type=random.nextInt(10)+1;
if (type==1) {
System.out.println("产生第"+i+"个客户,他是vip用户");
vips.put(new Consumer(i++, "vip"));
}else if (type<5) {
System.out.println("产生第"+i+"个客户,他是快速用户");
quicks.put(new Consumer(i++, "快速"));
}else {
System.out.println("产生第"+i+"个客户,他是普通用户");
normals.put(new Consumer(i++, "普通"));
}
Thread.sleep(time.nextInt(1000));
}
} catch (Exception e) {
// TODO: handle exception
}
}
public ArrayBlockingQueue getNormals() {
return normals;
}
public void setNormals(ArrayBlockingQueue normals) {
this.normals = normals;
}
public ArrayBlockingQueue getVips() {
return vips;
}
public void setVips(ArrayBlockingQueue vips) {
this.vips = vips;
}
public ArrayBlockingQueue getQuicks() {
return quicks;
}
public void setQuicks(ArrayBlockingQueue quicks) {
this.quicks = quicks;
}
}
T字母
/*package bank;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
public class Ts0 {
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(CustomerType. EXPRESS);
expressWindow.start();
// 产生1个VIP窗口
ServiceWindow vipWindow = new ServiceWindow();
vipWindow.setType(CustomerType. VIP);
vipWindow.start();
// 普通客户拿号
Executors. newScheduledThreadPool(1).scheduleAtFixedRate( new Runnable() {
public void run() {
Integer serviceNumber = NumMachine.getNumMachine()
.getCommonManager().addNum();
System. out.println("第" + serviceNumber + "号普通客户正在等待服务!" );
}
}, 0, Constants. COMMON_CUSTOMER_INTERVAL_TIME, TimeUnit.SECONDS );
// 快速客户拿号
Executors. newScheduledThreadPool(1).scheduleAtFixedRate( new Runnable() {
public void run() {
Integer serviceNumber = NumMachine.getNumMachine()
.getExpressManager().addNum();
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 = NumMachine.getNumMachine()
.getVipManager().addNum();
System. out.println("第" + serviceNumber + "号VIP客户正在等待服务!" );
}
}, 0, Constants. COMMON_CUSTOMER_INTERVAL_TIME * 6, TimeUnit.SECONDS );
}
}
* 服务窗口
class ServiceWindow {
private static Logger logger = Logger.getLogger("cn.itcast.bankqueue");
private CustomerType type = CustomerType.COMMON;
private int number = 1;
public CustomerType getType() {
return type ;
}
public void setType(CustomerType type) {
this.type = type;
}
public void setNumber(int number) {
this.number = number;
}
public void start() {
Executors. newSingleThreadExecutor().execute(new Runnable() {
public void run() {
// 下面这种写法的运行效率低,最好是把while放在case下面
while (true ) {
switch (type ) {
case COMMON:
commonService();
break;
case EXPRESS :
expressService();
break;
case VIP :
vipService();
break;
}
}
}
});
}
private void commonService() {
String windowName = "第" + number + "号" + type + "窗口" ;
System. out.println(windowName + "开始获取普通任务!" );
Integer serviceNumber = NumMachine.getNumMachine().getCommonManager()
.fetchNum();
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 expressService() {
Integer serviceNumber = NumMachine.getNumMachine().getExpressManager()
.fetchNum();
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 vipService() {
Integer serviceNumber = NumMachine.getNumMachine().getVipManager()
.fetchNum();
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();
}
}
}
* 客户类型
enum CustomerType {
COMMON, EXPRESS , VIP;
public String toString() {
String name = null;
switch (this ) {
case COMMON:
name = "普通";
break;
case EXPRESS :
name = "快速";
break;
case VIP :
name = name();
break;
}
return name;
}
}
* 排号器
class NumManager {
private int lastNum = 0;
private List queueNums = new ArrayList();
// 添加线程
public synchronized Integer addNum() {
queueNums.add(++lastNum);
return lastNum ;
}
// 取出线程
public synchronized Integer fetchNum() {
if (lastNum > 0) {
queueNums.remove(0);
return lastNum ;
}
return null ;
}
}
* 取号器(单例)
class NumMachine {
// 单例
private NumMachine() {
}
private static NumMachine numMachine = new NumMachine();
public static NumMachine getNumMachine() {
return numMachine ;
}
// 分三个排号器
private NumManager commonManager = new NumManager();
private NumManager expressManager = new NumManager();
private NumManager vipManager = new NumManager();
public NumManager getCommonManager() {
return commonManager ;
}
public NumManager getExpressManager() {
return expressManager ;
}
public NumManager getVipManager() {
return vipManager ;
}
}
* 客户
class Constants {
public static int MAX_SERVICE_TIME = 10000; // 10秒!
public static int MIN_SERVICE_TIME = 1000; // 1秒!
* 每个普通窗口服务一个客户的平均时间为5秒,一共有4个这样的窗口,也就是说银行的所有普通窗口合起来
* 平均1.25秒内可以服务完一个普通客户,再加上快速窗口和VIP窗口也可以服务普通客户,所以, 1秒钟产生一个普通客户比较合理,
public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;
}
*/
W字母:
package bank;
public abstract class Window {
public int id;
public String type;
public Boolean isBusy;
public void execute(Consumer client) {
System.out.println(id + "号" + type + "业务员 开始办理" + client.id + "号" + client.type + "顾客的业务");
isBusy = true;
client.execute();
isBusy = false;
System.out.println(id + "号normal业务员 办理完了" + client.id + "号" + client.type + "顾客的业务");
}
}
class WindowForVIP extends Window {
public WindowForVIP(int i) {
id = i;
type = "vip";
isBusy = false;
}
}
class WindowForNormal extends Window {
public WindowForNormal(int i) {
id = i;
type = "normal";
isBusy = false;
}
}
class WindowForQuick extends Window {
public WindowForQuick(int i) {
id = i;
type = "quick";
isBusy = false;
}
}
W字母
package bank;
import java.util.ArrayList;
import java.util.List;
public class WindowList {
public List normallist=new ArrayList<>();
public List viplist=new ArrayList<>();
public List quicklist=new ArrayList<>();
public WindowList(){
for(int i=1;i<5;i++)
normallist.add(new WindowForNormal(i));
quicklist.add(new WindowForQuick(5));
viplist.add(new WindowForVIP(6));
}
public Window getIdelVIPWindow(){
for(Window s:viplist){
if(!s.isBusy){
return s;
}
}
return null;
}
public Window getIdelNormalWindow(){
for(Window s:normallist){
if(!s.isBusy){
return s;
}
}
return null;
}
public Window getIdelQuickWindow(){
for(Window s:quicklist){
if(!s.isBusy){
return s;
}
}
return null;
}
}
然后开启mian方法就能模拟银行业务窗口