代码拉取完成,页面将自动刷新
package net.mindView.concurrency;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class BankTellerSimulation {
static final int MAX_LINE_SIZE = 50;
static final int ADJUSTMENT_PERIOD = 1000;
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
CustomerLine customers = new CustomerLine(MAX_LINE_SIZE);
exec.execute(new CustomerGenerator(customers));
exec.execute(new TellerManager(exec,customers,ADJUSTMENT_PERIOD));
TimeUnit.MILLISECONDS.sleep(3600);
exec.shutdownNow();
}
}
class Customer{
private final int serviceTime;
public Customer(int tm){ serviceTime = tm; }
public int getServiceTime(){ return serviceTime; }
@Override
public String toString() {
return "[" + serviceTime + "]";
}
}
//顾客的队伍
class CustomerLine extends ArrayBlockingQueue<Customer>{
public CustomerLine(int maxLineSize){
super(maxLineSize);
}
@Override
public String toString() {
if(this.size() == 0){
return "[Empty]";
}
StringBuilder result = new StringBuilder();
for(Customer customer: this)
result.append(customer);
return result.toString();
}
}
//产生随机顾客
class CustomerGenerator implements Runnable{
private CustomerLine customers;
private static Random rand = new Random(47);
public CustomerGenerator(CustomerLine cq){ customers = cq; }
public void run(){
try {
while (!Thread.interrupted()){
TimeUnit.MILLISECONDS.sleep(rand.nextInt(300));
//把customer加到customers里
//如果没有空间,则调用此方法的线程被阻断直到里面有空间再继续
customers.put(new Customer(rand.nextInt(1000)));
}
}catch (InterruptedException e){
System.out.println("CustomerGenerator interrupted");
}
System.out.println("CustomerGenerator terminating");
}
}
//出纳员
class Teller implements Runnable,Comparable<Teller>{
private static int count = 0;
private final int id = count++;
private int customersServed = 0; //已服务顾客数量
private CustomerLine customers;
private boolean servingCustomerLine = true;
public Teller(CustomerLine cq){ customers = cq; }
public void run(){
try{
while(!Thread.interrupted()){
//取走排在首位的对象;若为空,阻断进入等待状态直到有新的对象被加入为止
Customer customer = customers.take();
TimeUnit.MILLISECONDS.sleep(customer.getServiceTime());
synchronized (this){
customersServed++;
while (!servingCustomerLine)
wait();
}
}
}catch (InterruptedException e){
System.out.println(this + "interrupted");
}
System.out.println(this + "terminating");
}
public synchronized void doSomethingElse(){
customersServed = 0;
servingCustomerLine = false;
}
public synchronized void serveCustomerLine(){
assert !servingCustomerLine:"already serving:" + this;
servingCustomerLine = true;
notifyAll();
}
@Override
public String toString() {
return "Teller " + id;
}
public String shortString(){ return "T " + id; }
public synchronized int compareTo(Teller other){
return customersServed < other.customersServed ? -1 :
(customersServed == other.customersServed ? 0 : 1);
}
}
class TellerManager implements Runnable{
private ExecutorService exec;
private CustomerLine customers;
private PriorityQueue<Teller> workingTellers =
new PriorityQueue<Teller>();
private Queue<Teller> tellersDoingOtherThings =
new LinkedList<Teller>();
private int adjustmentPeriod;
private static Random rand = new Random(47);
public TellerManager(ExecutorService e,CustomerLine customers,int adjustmentPeriod){
exec = e;
this.customers = customers;
this.adjustmentPeriod = adjustmentPeriod;
//开启一个出纳员线程
Teller teller = new Teller(customers);
exec.execute(teller);
workingTellers.add(teller);
}
//调整出纳员数量,能够以稳定的方式添加或移除出纳员
public void adjustTellerNumber(){
//顾客数量变多
if(customers.size() / workingTellers.size() > 2){
//有空闲出纳员
if(tellersDoingOtherThings.size() > 0){
Teller teller = tellersDoingOtherThings.remove();
teller.serveCustomerLine();
workingTellers.offer(teller);
return;
}
//没有空闲出纳员时,创建出纳员
Teller teller = new Teller(customers);
exec.execute(teller);
workingTellers.add(teller);
return;
}
//顾客数量过少时,减少工作的出纳员
if(workingTellers.size() > 1 && (customers.size() / workingTellers.size() < 2)){
reassignOneTeller();
}
//没有顾客时,减少工作的出纳员
if(customers.size() == 0)
while (workingTellers.size() > 1)
reassignOneTeller();
}
private void reassignOneTeller(){
Teller teller = workingTellers.poll();
teller.doSomethingElse();
tellersDoingOtherThings.add(teller);
}
public void run(){
try {
while(!Thread.interrupted()){
TimeUnit.MILLISECONDS.sleep(adjustmentPeriod);
adjustTellerNumber();
System.out.print(customers + "{ ");
for(Teller teller: workingTellers)
System.out.print(teller.shortString() + " ");
System.out.println(" }");
}
}catch (InterruptedException e){
System.out.println(this + "interrupted");
}
System.out.println(this + "terminating");
}
@Override
public String toString() {
return "TellerManager ";
}
}
/*Output:
[200][207]{ T 1 T 0 }
[861][258][140][322]{ T 1 T 0 }
[575][342][804][826][896][984]{ T 2 T 0 T 1 }
TellerManager interrupted
Teller 1interrupted
Teller 2interrupted
Teller 0interrupted
CustomerGenerator interrupted
Teller 0terminating
Teller 2terminating
Teller 1terminating
TellerManager terminating
CustomerGenerator terminating
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。