Developed a multi-thread based Java program for a small barbershop. Customer and Barber arrive at random intervals, with mean and standard deviation. Each barber has a number of chairs in which the customer sits when they are getting their hair cut, this application which was developed in java using LinkedList.
1. 1 | P a g e 6
CA670 Concurrent Programming
Name Archana Kalapgar
Student Number 19210184
Programme MCM (Cloud)
Module Code CA670
Assignment Title Assignment One - Java Threads
Submission date 17-March-2020
Module coordinator David Sinclair
I declare that this material, which I now submit for assessment, is entirely my work and has
not been taken from the work of others, save and to the extent that such work has been cited
and acknowledged within the text of my work. I understand that plagiarism, collusion, and
copying are grave and serious offences in the university and accept the penalties that would be
imposed should I engage in plagiarism, collusion or copying. I have read and understood the
Assignment Regulations set out in the module documentation. I have identified and included
the source of all facts, ideas, opinions, and viewpoints of others in the assignment references.
Direct quotations from books, journal articles, internet sources, module text, or any other
source whatsoever are acknowledged, and the source cited are identified in the assignment
references. This assignment, or any part of it, has not been previously submitted by me or any
other person for assessment on this or any other course of study.
I have read and understood the referencing guidelines found recommended in the assignment
guidelines.
Name: Archana Kalapgar
Date: 17-March-2020
2. 2 | P a g e 6
Assignment One - Java Threads
Problem Statement:
We need to develop a multi-thread based Java program for a small barbershop that has two
doors, an entrance and an exit. Inside is a set of M barbers who spends all their lives serving
customers, one at a time. Each barber has a chair in which the customer sits when they are
getting their hair cut. When there are no customers in the shop waiting for their hair to be cut,
a barber sleeps in his chair. Customer arrives at random intervals, with mean mc and standard
deviation sdc. If a customer arrives and finds the barber asleep, he awakens the barber, sits in
the barber’s chair and sleeps while his hair is being cut. The time taken to cut a customer's hair
has a mean mh and standard deviation sdh. If a customer arrives and all the barbers are busy
cutting hair, the customer goes asleep in one of the N waiting chairs. When the barber finishes
cutting a customer’s hair, he awakens the customer and holds the exit door open for him. If
there are any waiting customers, he awakens one and waits for the customer to sit in the barber's
chair, otherwise, he goes to sleep.
The application must be:
• correct,
• fair,
• mutual exclusive,
• have no deadlock, and
• not have any individual thread "starved".
The application should be efficient concerning all actors.
Design of the application:
I developed a multi-threading Java application which is capable of running on a multiple-core
system. This application is capable of handling (n) number of barber and (m) number of
customers. It is also possible to increase the number of barber and customers accordingly.
Technologies Used:
1. Java for Multithreading
Java application consists of one package and four different classes. Below table
Represents the structure of the application.
Package Class
threading.practice SleepingBarber
Barber
Customer
BarberShop
3. 3 | P a g e 6
A brief description of each class is given below[1]:
1. SleepingBarber: This is the main class of application. This class is written explicitly
to take user input of the number of barber/s (barberCount), the number of customer/s
(customerCount), Mean (mean1), Standard Deviation (std1) for the duration of time
taken to cut a customer/s hair. Customer/s arrives at random intervals with runtime
mean (mean2) and standard deviation (std2). Barbershop has the number of waiting
chairs for the customer, which is also taken from the user (nChairs). All the methods
are made dynamic because they can be called creating the instance of the class where
it resides. A new object of the shop is created in the main class. New barber object is
created passing (mean1), (std1), and the object shop() to it. New customer() object is
created, and the customer arrival rate is set randomly by gaussian formula.
2. Barber: This class implements the following methods: getId(), setId(),
getBarberCuttingDurationMean(), SetBarberCuttingDurationMean(),
SetBarberCuttingDurationStdDev(), SetBarberCuttingDurationStdDev(). The run
method will call cutHair() method of object shop.
3. Customer: This class implements the following methods: getName(), setName(). The
entry of customer is added one at a time due to synchronize method invoking add
method for object shop(). When you have a synchronized method, you are locking on
the instance of the enclosing class. The run method will call goForHairCut() method.
4. Barbershop: This is the important class of the application. This class implements the
following methods: getnChair(), setnChair(). We create a linked list for the waiting
chair to preserve the order of insertion of the incoming customer. We pass (mean1) and
(std1) to getInterval() method and randomly generate duration of the barber to
cutHair(). Synchronize method makes sure to serve first customer thread at a time by
first barber thread. If there is no customer, the barber will wait for the customer. If there
are customer, then barber will take the first customer from the list. Add() method will
add customers in the list and synchronize method will make sure that first customer
thread sits on the waiting chair at a time. This class also creates tasks based on the
inputs sent by the constructor. It implements Runnable interface. If all the barber and
customer threads are busy and the waiting chair are occupied then the customer thread
exits from the loop.
Application design highlights [1] [2] [3] [4]:
1. Absence of Deadlock and Thread starvation: Inter thread communication is used to
prevent deadlocks. At a time, only one thread can access a synchronized method. As a
particular customer enters in the linked-list, a specific barber gets notify and gets the
lock. Barber finds the customer in the queue and performs haircut, completes haircut
and waits for lock again to perform same operations. This avoids deadlock. The
customers who come first are served first, this avoids starvation.
2. Correctness: Input is validated first then the barber and customer thread start to
execute. If all the barber and waiting chair are busy, customer exits from loop, else,
haircut is performed. Many other screenshots which confirm the correctness of the
application are submitted along with this report.
4. 4 | P a g e 6
3. Fairness: The barber thread, which is created first, gets the lock first. The customer
thread, which enters the linked list first is served first, which ensures the fairness of
application. Although the duration of haircut depends upon random variable based on
user input mean and standard deviation. Thus, the customer who started haircut first
may or may not end up finishing first.
4. Efficiency: The application works efficiently and serves all the customer/s in the list,
if number of customer/s (m) is double the number of barber/s (n) i.e (m=2*n) and
number of waiting chair is equal to the number of barber/s (n) i.e (waiting chair = n),
as shown in test cases [1 to 5].
A description of where a solution to the sleeping barber(s) problem is used
in practice:
1. Movie Ticket Booking: In an online movie ticket booking, the customer arrives in a
queue as first come first serve. A customer chooses a seat and that seat is asleep for 15
minutes until the payment confirmation. Once a booking is confirmed the customer
completes the task if the booking is failed then the seat is asleep for 15 minutes and no
customer can book that particular seat for the time interval. This is a synchronous
example of multithreading similar to sleeping barber problem.
2. AI Self Driving Car[4]: In an AI-based self-driving car, it is important to apply
sleeping barber problem. When a person is entering into the car, the car should wakeup
from sleep ready to function. If there is no person, a car should go back to sleep. This
is a real-life application of the sleeping barber problem.
Test Cases: In test cases [1 to 5], no customer exits from the loop and the barber completes
the haircut of all the customer threads. In test case [6], the barber was busy and the waiting
chairs were occupied, so some customer threads exit from the loop.
1. Barber: 5, Std1: 2, Mean1: 4, Customer: 10, Std2: 3, Mean2: 1, Waiting chair:
5, shown in the Image 1 -PASS.
2. Barber: 10, Std1: 3, Mean1: 1, Customer: 20, Std2: 2, Mean2: 4, Waiting chair:
10, shown in the Image 2- PASS.
3. Barber: 15, Std1: 4, Mean1: 4, Customer: 30, Std2: 4, Mean2: 4, Waiting chair:
15, screenshot submitted in the folder named “screenshots”- PASS.
4. Barber: 1000, Std1: 4, Mean1: 3, Customer: 2000, Std2: 3 Mean2: 4, Waiting
chair: 1000, screenshot submitted in the folder named “screenshots”- PASS.
5. Barber: 10000, Std1: 1, Mean1: 2, Customer: 20000, Std2: 3, Mean2: 4, Waiting
chair: 10000, screenshot submitted in the folder named “screenshots”- PASS.
6. Barber: 1, Std1: 7, Mean1: 9, Customer: 10, Std2: 3, Mean2: 1, Waiting chair:
5, screenshot submitted in the folder named “screenshots”- PASS.