package com.test;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
/**
* @see 服务中心
* @author Herman.Xiong
* @date 2014年11月17日 14:41:28
* @version V1.0
*/
public class Center extends Thread {
private final static int MAXCOUNT = 10;
private BlockingQueue<Waiter> waiters;
private BlockingQueue<Customer> customers;
private Random rand = new Random(47);
private final static int PRODUCERSLEEPSEED = 100;
private final static int CONSUMERSLEEPSEED = 10000;
public Center() {
//创建10名提供服务的工作人员
this.waiters = new LinkedBlockingQueue<Waiter>(MAXCOUNT);
for (int i = 0; i < MAXCOUNT; i++) {
waiters.add(new Waiter());
}
//10名工作人员工作就绪,创建客户队列
this.customers = new LinkedBlockingQueue<Customer>();
}
public void produce() {
try {
TimeUnit.MILLISECONDS.sleep(rand.nextInt(PRODUCERSLEEPSEED));
} catch (InterruptedException e) {
e.printStackTrace();
}
this.customers.add(new Customer());
}
public void consume() {
try {
// 服务窗口可用
Waiter waiter = this.waiters.take();
this.waiters.remove(waiter);
// 客户可用
Customer customer = this.customers.take();
this.customers.remove(customer);
// 窗口显示
System.out.println(waiter + "正在为" + customer + "服务...");
TimeUnit.MILLISECONDS.sleep(rand.nextInt(CONSUMERSLEEPSEED));
this.waiters.add(waiter);
} catch (InterruptedException e) {
System.err.println("---" + e.getMessage());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
queueDemo.zip (15个子文件)
queueDemo
bin
com
test
Center.class 2KB
Consumer.class 596B
Customer.class 818B
CallNum.class 1KB
Producer.class 596B
Waiter.class 808B
.settings
org.eclipse.jdt.core.prefs 629B
src
com
test
Waiter.java 291B
Producer.java 518B
Center.java 2KB
CallNum.java 936B
Customer.java 308B
Consumer.java 471B
.project 385B
.classpath 301B
共 15 条
- 1
业余草
- 粉丝: 1w+
- 资源: 979
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页