SlideShare a Scribd company logo
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 | 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 | 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 | 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.
5 | P a g e 6
Image 1
Image 2
6 | P a g e 6
REFERENCES
[1] "Concurrency-Oracle," [Online]. Available: https://orajavasolutions.wordpress.com/tag/sleeping-
barber-problem/. [Accessed 01 03 2020].
[2] D. Sinclair, "CA670-Java Threads," [Online]. Available:
https://www.computing.dcu.ie/~davids/courses/CA670/CA670_Java_Threads_2p.pdf. [Accessed
01 03 2020].
[3] " Geeksforgeeks," [Online]. Available: https://www.geeksforgeeks.org/random-nextgaussian-
method-in-java-with-examples/. [Accessed 10 03 2020].
[4] "Aitrends," [Online]. Available: https://www.aitrends.com/ai-insider/sleeping-barber-problem-
and-ai-self-driving-cars/. [Accessed 12 03 2020].

More Related Content

Similar to Sleeping barber

10820141MATLAB Introduction IIProgramming and Sc.docx
10820141MATLAB Introduction IIProgramming and Sc.docx10820141MATLAB Introduction IIProgramming and Sc.docx
10820141MATLAB Introduction IIProgramming and Sc.docx
hyacinthshackley2629
 
note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptx
ReemaAsker1
 
Design and development of industrial End effector (gripper)
Design and development of industrial End effector (gripper)Design and development of industrial End effector (gripper)
Design and development of industrial End effector (gripper)
Hrishikesh Mahajan
 
Final Report
Final ReportFinal Report
Final ReportAman Soni
 
Operating Systems lab Programs Algorithm - Fourth Semester - Engineering
Operating Systems lab Programs Algorithm - Fourth Semester - EngineeringOperating Systems lab Programs Algorithm - Fourth Semester - Engineering
Operating Systems lab Programs Algorithm - Fourth Semester - Engineering
Yogesh Santhan
 
C question-answer-bank
C question-answer-bankC question-answer-bank
C question-answer-bank
REHAN KHAN
 
Ghar Service
Ghar ServiceGhar Service
Ghar Service
Manish Tuladhar
 
A MULTIPLE – CHANNEL QUEUING MODELS ON FUZZY ENVIRONMENT
A MULTIPLE – CHANNEL QUEUING MODELS ON FUZZY ENVIRONMENTA MULTIPLE – CHANNEL QUEUING MODELS ON FUZZY ENVIRONMENT
A MULTIPLE – CHANNEL QUEUING MODELS ON FUZZY ENVIRONMENT
IAEME Publication
 
Ha2512781284
Ha2512781284Ha2512781284
Ha2512781284
IJERA Editor
 
Lecture_four-_Requirements_Modeling (1).pptx
Lecture_four-_Requirements_Modeling (1).pptxLecture_four-_Requirements_Modeling (1).pptx
Lecture_four-_Requirements_Modeling (1).pptx
GracePeter10
 
IRJET- Automatic Suggestion of Outfits using Image Processing
IRJET- Automatic Suggestion of Outfits using Image ProcessingIRJET- Automatic Suggestion of Outfits using Image Processing
IRJET- Automatic Suggestion of Outfits using Image Processing
IRJET Journal
 
07_AJMS_392_22_Revised.pdf
07_AJMS_392_22_Revised.pdf07_AJMS_392_22_Revised.pdf
07_AJMS_392_22_Revised.pdf
BRNSS Publication Hub
 
IRJET- New Generation Multilevel based Atm Security System
IRJET- New Generation Multilevel based Atm Security SystemIRJET- New Generation Multilevel based Atm Security System
IRJET- New Generation Multilevel based Atm Security System
IRJET Journal
 
C question-bank-ebook
C question-bank-ebookC question-bank-ebook
C question-bank-ebooketrams1
 
Revised le blanc_ltec_5120_old_new_rubric
Revised le blanc_ltec_5120_old_new_rubricRevised le blanc_ltec_5120_old_new_rubric
Revised le blanc_ltec_5120_old_new_rubric
Laura Leblanc
 
Case Study for Bank ATM Queuing Model
Case Study for Bank ATM Queuing ModelCase Study for Bank ATM Queuing Model
Case Study for Bank ATM Queuing Model
IOSR Journals
 

Similar to Sleeping barber (17)

10820141MATLAB Introduction IIProgramming and Sc.docx
10820141MATLAB Introduction IIProgramming and Sc.docx10820141MATLAB Introduction IIProgramming and Sc.docx
10820141MATLAB Introduction IIProgramming and Sc.docx
 
note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptx
 
Design and development of industrial End effector (gripper)
Design and development of industrial End effector (gripper)Design and development of industrial End effector (gripper)
Design and development of industrial End effector (gripper)
 
Final Report
Final ReportFinal Report
Final Report
 
Operating Systems lab Programs Algorithm - Fourth Semester - Engineering
Operating Systems lab Programs Algorithm - Fourth Semester - EngineeringOperating Systems lab Programs Algorithm - Fourth Semester - Engineering
Operating Systems lab Programs Algorithm - Fourth Semester - Engineering
 
C question-answer-bank
C question-answer-bankC question-answer-bank
C question-answer-bank
 
Ghar Service
Ghar ServiceGhar Service
Ghar Service
 
A MULTIPLE – CHANNEL QUEUING MODELS ON FUZZY ENVIRONMENT
A MULTIPLE – CHANNEL QUEUING MODELS ON FUZZY ENVIRONMENTA MULTIPLE – CHANNEL QUEUING MODELS ON FUZZY ENVIRONMENT
A MULTIPLE – CHANNEL QUEUING MODELS ON FUZZY ENVIRONMENT
 
Ha2512781284
Ha2512781284Ha2512781284
Ha2512781284
 
Ha2512781284
Ha2512781284Ha2512781284
Ha2512781284
 
Lecture_four-_Requirements_Modeling (1).pptx
Lecture_four-_Requirements_Modeling (1).pptxLecture_four-_Requirements_Modeling (1).pptx
Lecture_four-_Requirements_Modeling (1).pptx
 
IRJET- Automatic Suggestion of Outfits using Image Processing
IRJET- Automatic Suggestion of Outfits using Image ProcessingIRJET- Automatic Suggestion of Outfits using Image Processing
IRJET- Automatic Suggestion of Outfits using Image Processing
 
07_AJMS_392_22_Revised.pdf
07_AJMS_392_22_Revised.pdf07_AJMS_392_22_Revised.pdf
07_AJMS_392_22_Revised.pdf
 
IRJET- New Generation Multilevel based Atm Security System
IRJET- New Generation Multilevel based Atm Security SystemIRJET- New Generation Multilevel based Atm Security System
IRJET- New Generation Multilevel based Atm Security System
 
C question-bank-ebook
C question-bank-ebookC question-bank-ebook
C question-bank-ebook
 
Revised le blanc_ltec_5120_old_new_rubric
Revised le blanc_ltec_5120_old_new_rubricRevised le blanc_ltec_5120_old_new_rubric
Revised le blanc_ltec_5120_old_new_rubric
 
Case Study for Bank ATM Queuing Model
Case Study for Bank ATM Queuing ModelCase Study for Bank ATM Queuing Model
Case Study for Bank ATM Queuing Model
 

Recently uploaded

RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 

Recently uploaded (20)

RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 

Sleeping barber

  • 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.
  • 5. 5 | P a g e 6 Image 1 Image 2
  • 6. 6 | P a g e 6 REFERENCES [1] "Concurrency-Oracle," [Online]. Available: https://orajavasolutions.wordpress.com/tag/sleeping- barber-problem/. [Accessed 01 03 2020]. [2] D. Sinclair, "CA670-Java Threads," [Online]. Available: https://www.computing.dcu.ie/~davids/courses/CA670/CA670_Java_Threads_2p.pdf. [Accessed 01 03 2020]. [3] " Geeksforgeeks," [Online]. Available: https://www.geeksforgeeks.org/random-nextgaussian- method-in-java-with-examples/. [Accessed 10 03 2020]. [4] "Aitrends," [Online]. Available: https://www.aitrends.com/ai-insider/sleeping-barber-problem- and-ai-self-driving-cars/. [Accessed 12 03 2020].