Философия Java3
вернуться

Эккель Брюс

Шрифт:

while(IThread.interruptedO) {

TimeUnit MILLISECONDS.sleep(rand nextlnt(300)):

продолжение &

customers put(new Customer(rand nextlnt(lOOO)));

}

} catchdnterruptedException e) {

System.out.pri ntin("CustomerGenerator i nterrupted");

}

System.out printin("CustomerGenerator terminating");

class Teller implements Runnable. Comparable<Teller> { private static int counter = 0; private final int id = counter**; // Счетчик клиентов, обслуженных за текущую смену: private int customersServed = 0; private CustomerLine customers; private boolean servingCustomerLine = true; public Teller(CustomerLine cq) { customers = cq; } public void run О { try {

while(IThread.interruptedO) {

Customer customer = customers.takeO. Ti meUni t.MILLISECONDS.s1eep(

customer. getServiceTimeO); synchronized(this) {

customersServed++; while(IservingCustomerLine) waitO;

}

}

} catchdnterruptedException e) {

System out println(this + "прерван");

}

System out.println(this + "завершается");

}

public synchronized void doSomethingElseO { customersServed = 0; servingCustomerLine = false;

}

public synchronized void serveCustomerLineO {

assert IservingCustomerLine:"уже обслуживает: " + this; servingCustomerLine = true; notifyAl 10;

}

public String toStringO { return "Кассир " + id + " "; } public String shortStringO { return "K" + 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<Tel 1 er>; 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 adjustTellerNumberO {

// Фактически это система управления. Регулировка числовых // параметров позволяет выявить проблемы стабильности // в механизме управления.

// Если очередь слишком длинна, добавить другого кассира: if(customers.size / workingTellers.sizeO > 2) { // Если кассиры отдыхают или заняты // другими делами, вернуть одного из них: if(tellersDoingOtherThings.size > 0) {

Teller teller = tellersDoingOtherThings.remove; tel1er.serveCustomerLi ne; workingTellers.offer(teller); return;

}

// Иначе создаем (нанимаем) нового кассира Teller teller = new Teller(customers); exec.execute(teller); workingTellers.add(teller); return;

}

// Если очередь достаточно коротка, освободить кассира: if (workingTellers.sizeO > 1 &&

customers.size / workingTellers.sizeO < 2) reassignOneTellerO; // Если очереди нет. достаточно одного кассира: if (customers, si ze ==0)

while(workingTellers.size > 1) reassignOneTellerO;

}

// Поручаем кассиру другую работу или отправляем его отдыхать: private void reassignOneTellerO {

Teller teller = workingTellers.pollО;

tel 1 er. doSomethi ngEl seO,

tel1ersDoi ngOtherThi ngs.offer(tel1er);

}

public void runO { try {

while(!Thread.interruptedO) {

TimeUnit.MILLISECONDS.sleep(adjustmentPeriod);

adjustTellerNumberO;

System.out.print(customers +"{");

for(Teller teller workingTellers)

System.out.print(teller.shortString + " "); System.out.printIn("}"); } продолжение &

} catchdnterruptedException е) {

System.out.printin(this + "прерван");

}

System.out println(this + "завершается");

}

public String toStringO { return "TellerManager "; }

}

public class BankTellerSimulation {

static final int MAX_LINE_SIZE = 50;

static final int ADJUSTMENT_PERIOD = 1000;

  • Читать дальше
  • 1
  • ...
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • ...

Private-Bookers - русскоязычная библиотека для чтения онлайн. Здесь удобно открывать книги с телефона и ПК, возвращаться к сохраненной странице и держать любимые произведения под рукой. Материалы добавляются пользователями; если считаете, что ваши права нарушены, воспользуйтесь формой обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • help@private-bookers.win