Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

import java.util.concurrent.

BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

public class Customer extends Thread {

CyclicBarrier standByMessage;
private Semaphore operators;

public Customer(Semaphore operators, CyclicBarrier standByMessage,


String name) {
this.standByMessage = standByMessage;
this.operators = operators;
this.setName(name);
this.start();
}

@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());

} catch (InterruptedException | BrokenBarrierException e) {


System.err.println(e);
}

}
}
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!

The call center has 3 operators:

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

The Cyclic barrier takes a Thread as argument for the constructor:

1 public class StandBy implements Runnable {


2
3 @Override
4 public void run() {
System.out.println("All operators are busy at the moment - playing MUSIC
5 }
6
7 }
8

The output is something like:

The call center is ready to answer phone calls


Available operators initially=3
Alfonso is waiting to speak to the operator...
Laura is waiting to speak to the operator...
Paola is waiting to speak to the operator...
Luigi is waiting to speak to the operator...
Anna is waiting to speak to the operator...
Mario is waiting to speak to the operator...
Giorgio is waiting to speak to the operator...
All operators are busy at the moment - playing MUSIC ...
Francesca is waiting to speak to the operator...
Pietro is waiting to speak to the operator...
All operators are busy at the moment - playing MUSIC ...
Paola is getting the connection to the operator ...
Giovanna is waiting to speak to the operator...
Luigi is getting the connection to the operator ...
Marco is waiting to speak to the operator...
Antonio is waiting to speak to the operator...
Giorgio is waiting to speak to the operator...
All operators are busy at the moment - playing MUSIC ...
Daniele is waiting to speak to the operator...
Mario is getting the connection to the operator ...
All operators are busy at the moment - playing MUSIC ...
Luigi´s phone call with the operator ending.
Available operators=1
Alfonso is getting the connection to the operator ...
Mario´s phone call with the operator ending.
Available operators=1
Laura is getting the connection to the operator ...
Paola´s phone call with the operator ending.
Available operators=1
Francesca is getting the connection to the operator ...
Alfonso´s phone call with the operator ending.
Available operators=1
Pietro is getting the connection to the operator ...
Laura´s phone call with the operator ending.
Available operators=1
Marco is getting the connection to the operator ...
Francesca´s phone call with the operator ending.
Available operators=1
Anna is getting the connection to the operator ...
Pietro´s phone call with the operator ending.
Available operators=1
Giovanna is getting the connection to the operator ...
Anna´s phone call with the operator ending.
Available operators=1
Daniele is getting the connection to the operator ...
Marco´s phone call with the operator ending.
Available operators=1
Giorgio is getting the connection to the operator ...
Giorgio´s phone call with the operator ending.
Available operators=1
Giovanna´s phone call with the operator ending.
Available operators=2
Daniele´s phone call with the operator ending.
Available operators=3

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;
}

public int getCustomerNumber() {


return customerNumber;
}

public String geteMail() {


return eMail;
}

public void setCustomerNumber(int customerNumber) {


this.customerNumber = customerNumber;
}

public void seteMail(String eMail) {


this.eMail = eMail;
}

public boolean getMailingList()


{
return onMailingList;
{

// ****************** Tester Class ****************


public class CustomerDemo {

public static void main (String [] Args){

Customer customer = new Customer("Adam Stanley",818224841,"455 Larkspur Dr.", 1,


"adamstanley@gmail.com", true );

System.out.println(" Name Phone Number Address Customer Number Customer E-Mail On


ML");
System.out.println("__________________...
System.out.println(" " + customer.getName() + " " + customer.getphoneNumber() + " "
+ customer.getaddress() + " " + customer.getCustomerNumber() + " " + customer.geteMail()
+ " " + customer.getMailingList);

Customer Class:

import java.util.Scanner;

public class customer {


//create variables
private int Id;
private String firstName;
private String lastName;
private long phoneNo;

public customer() {

//setters and getters


public int getId() {
return Id;
}

public void setId(int id) {


Id = id;
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) throws InputValidationException {


if (firstName.matches("\\p{Upper}(\\p{Lower}){2,20}")) {
} else {
throw new InputValidationException();
}
this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) throws InputValidationException {


if (lastName.matches("\\p{Upper}(\\p{Lower}){2,20}")) {
} else {
throw new InputValidationException();
}
this.lastName = lastName;
}

public long getPhoneNo() {


return phoneNo;
}

public void setPhoneNo(long phoneNo) {


this.phoneNo = phoneNo;
}
//constructor
public customer(int Id, String firstName, String lastName, long phoneNo) {
this.Id = Id;
this.firstName = firstName;
this.lastName = lastName;
this.phoneNo = phoneNo;

//get user input


public void customerInfo() {
Scanner input = new Scanner(System.in);

{
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);

System.out.println("Enter first name:");


String firstName = input.nextLine();

System.out.println("Enter last name:");


String lastName = input.nextLine();

System.out.println("Enter phone number:");


long phoneNo = Long.parseLong(input.nextLine());

//add customer
customers.add(new customer(id, firstName, lastName,phoneNol));

}
}

public void displayCustomers() {}


//Display All
public String toString() {
return '{' +
"id: " + Id +
", firstName: " + firstName +
", lastName: " + lastName +
", address: " + address +
", phoneNo: " + phoneNo +
'}';
}
}
Customers Class:

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>();
}

public static void add(customer customer) {


}

public static void get()

{
//Display All
System.out.println("Current List: ");
for (customer customer : customers) {
System.out.println(customer.toString());
}
}
}

Main Class:

public class Main {


public static void main(String[] args) {

customer customerInfoObject = new customer();


customerInfoObject.customerInfo();

customer displayCustomersObject = new customer(); {


displayCustomersObject.displayCustomers();
}

}
}

You might also like