Professional Documents
Culture Documents
Import Java
Import Java
BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;
CyclicBarrier standByMessage;
private Semaphore operators;
@Override
public void run() {
// simulate the time required for the phone call (between one and six seconds...
long duration = ThreadLocalRandom.current().nextLong(1,10);
try {
System.out.println(getName()
+ " is waiting to speak to the operator...");
standByMessage.await();
// if you put acquire first and then await the operators are not
// free and all the customers are not called
operators.acquire();
System.out.println(getName()
+ " is getting the connection to the operator ...");
//using TimeUnit enumeration to make the code more readable
Thread.sleep(TimeUnit.SECONDS.toMillis(duration));
System.out.println(getName()
+ "´s phone call with the operator ending.");
operators.release();
System.out.println("Available operators="
+ operators.availablePermits());
}
}
Take a look at the way the duration of the call is generated. I am using the
ThreadLocalRandom class, that in a multithreading context it´s helpful to get the
job done much faster.
At first I tried it with Math.random() and it was MUCH slower!
1
2
3 import java.util.concurrent.CyclicBarrier;
4 import java.util.concurrent.Semaphore;
5
public class CallCenter {
6
7 public static void main(String[] args) {
8
9 Semaphore operators = new Semaphore(3);
10
11 CyclicBarrier standbyMessage = new CyclicBarrier(3, new StandBy());
12
13 System.out.println("The call center is ready to answer phone calls&
14
15 System.out.println("Available operators initially=" + oper
16
new Customer(operators,standbyMessage,"Laura");
17
new Customer(operators,standbyMessage,"Mario");
18 new Customer(operators,standbyMessage,"Luigi");
19 new Customer(operators,standbyMessage,"Paola");
20 new Customer(operators,standbyMessage,"Alfonso");
21 new Customer(operators,standbyMessage,"Anna");
new Customer(operators,standbyMessage,"Giorgio");
22 new Customer(operators,standbyMessage,"Francesca");
23 new Customer(operators,standbyMessage,"Pietro");
24 new Customer(operators,standbyMessage,"Antonio");
25 new Customer(operators,standbyMessage,"Marco");
26 new Customer(operators,standbyMessage,"Giovanna");
new Customer(operators,standbyMessage,"Daniele");
27 new Customer(operators,standbyMessage,"Giorgio");
28 }
29
30 }
31
32
public Customer()
{
customerNumber = 0;
eMail = "";
}
public Customer (String n, int pn, String ad, int cn, String em, boolean ml)
{
super(n,pn,ad);
customerNumber = cn;
eMail = em;
onMailingList = ml;
}
Customer Class:
import java.util.Scanner;
public customer() {
{
while (true) {
//ask user for input and get input
System.out.println("Enter id (press 'q' to quit): ");
String temp = input.nextLine();
if (temp.equals("q")) break;
int id = Integer.parseInt(temp);
//add customer
customers.add(new customer(id, firstName, lastName,phoneNol));
}
}
import java.util.ArrayList;
//creates and array of the customers
public final class customers {
public static ArrayList<customer> customers;
public customers() {
customers = new ArrayList<customer>();
}
{
//Display All
System.out.println("Current List: ");
for (customer customer : customers) {
System.out.println(customer.toString());
}
}
}
Main Class:
}
}