银行排队的简单实现

这段时间在看Java并发编程方面的东西,注意到“生产者-消费者”模式,去某公司笔试的时候也遇到了这样的题,今天顺便把他用程序的方式写了下来。

UML就免了,不想画!顺便吐槽一下,小组开发,一定得用UML吗?随便画点图不行么?)

先上ServiceManager,它相当于大厅里的排号机,客户自己去排号,然后柜台的服务人员会去自动的取号:

Java代码 复制代码 收藏代码
  1. /**
  2. *
  3. */
  4. package com.fcm.thread.banksample;
  5. import java.util.concurrent.BlockingQueue;
  6. import java.util.concurrent.PriorityBlockingQueue;
  7. /**
  8. * @author fenggcai
  9. *
  10. */
  11. public class ServiceManager {
  12. /**
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. //only 50 customers could be served a time
  17. final BlockingQueue<FIFOEntry<Customer>> customerQueue = new PriorityBlockingQueue<FIFOEntry<Customer>>(
  18. 50);
  19. for (int i = 0; i < 52; i++) {
  20. int randomLevel = new Double(Math.random() * 5).intValue();
  21. Customer c = new Customer("Customer" + i,
  22. CustomerLevel.values()[randomLevel]);
  23. new Thread(new CustomerService(customerQueue, c)).start();
  24. }
  25. for (int i = 0; i < 5; i++) {
  26. BankService bs = new BankService(customerQueue);
  27. new Thread(bs).start();
  28. try {
  29. Thread.currentThread().sleep(1000);
  30. } catch (InterruptedException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值