package demo2;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
/*服务中心*/
public class Center extends Thread {
private BlockingQueue<Waiter> waiters;
private BlockingQueue<Customer> customers;
private final static int PRODUCERSLEEPSEED = 3000;
private final static int CONSUMERSLEEPSEED = 100000;
public Center(int num) {
//创建提供服务的柜台队列和取得号的客户队列
waiters = new LinkedBlockingQueue<Waiter>(num);
customers = new LinkedBlockingQueue<Customer>(num);
}
//取号机产生新号码
public void produce() throws InterruptedException {
Customer c_temp = new Customer();
Waiter w_temp = new Waiter();
customers.put(c_temp);
waiters.put(w_temp);
System.out.println(c_temp + " is waiting for service!");
Random rand = new Random();
TimeUnit.MILLISECONDS.sleep(rand.nextInt(PRODUCERSLEEPSEED));
}
//客户获得服务
public void consume() throws InterruptedException {
Customer c_temp = customers.take();
Waiter w_temp = waiters.take();
System.out.println(w_temp+" is serving " +c_temp);
Random rand = new Random();
TimeUnit.MILLISECONDS.sleep(rand.nextInt(CONSUMERSLEEPSEED));
}
}