SlideShare a Scribd company logo
1 of 6
Download to read offline
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

Use cases - As approach to building shared vision
Use cases - As approach to building shared visionUse cases - As approach to building shared vision
Use cases - As approach to building shared visionAbhilash Gopalakrishnan
 
10820141MATLAB Introduction IIProgramming and Sc.docx
10820141MATLAB Introduction IIProgramming and Sc.docx10820141MATLAB Introduction IIProgramming and Sc.docx
10820141MATLAB Introduction IIProgramming and Sc.docxhyacinthshackley2629
 
note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxReemaAsker1
 
Project crm submission sonali
Project crm submission sonaliProject crm submission sonali
Project crm submission sonaliSonali Gupta
 
013 Internet Essay Example Thatsnotus. Online assignment writing service.
013 Internet Essay Example  Thatsnotus. Online assignment writing service.013 Internet Essay Example  Thatsnotus. Online assignment writing service.
013 Internet Essay Example Thatsnotus. Online assignment writing service.Danielle Davis
 
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 - EngineeringYogesh Santhan
 
C question-answer-bank
C question-answer-bankC question-answer-bank
C question-answer-bankREHAN KHAN
 
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 ENVIRONMENTIAEME Publication
 
Lecture_four-_Requirements_Modeling (1).pptx
Lecture_four-_Requirements_Modeling (1).pptxLecture_four-_Requirements_Modeling (1).pptx
Lecture_four-_Requirements_Modeling (1).pptxGracePeter10
 
Simulation of Great Clips Salon
Simulation of Great Clips SalonSimulation of Great Clips Salon
Simulation of Great Clips SalonPrerit Saxena
 
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 ProcessingIRJET Journal
 
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 SystemIRJET Journal
 
C question-bank-ebook
C question-bank-ebookC question-bank-ebook
C question-bank-ebooketrams1
 

Similar to Sleeping barber (19)

Use cases - As approach to building shared vision
Use cases - As approach to building shared visionUse cases - As approach to building shared vision
Use cases - As approach to building shared vision
 
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
 
Project crm submission sonali
Project crm submission sonaliProject crm submission sonali
Project crm submission sonali
 
013 Internet Essay Example Thatsnotus. Online assignment writing service.
013 Internet Essay Example  Thatsnotus. Online assignment writing service.013 Internet Essay Example  Thatsnotus. Online assignment writing service.
013 Internet Essay Example Thatsnotus. Online assignment writing service.
 
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
 
Simulation of Great Clips Salon
Simulation of Great Clips SalonSimulation of Great Clips Salon
Simulation of Great Clips Salon
 
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
 

Recently uploaded

Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Paige Cruz
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftshyamraj55
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxMasterG
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)Wonjun Hwang
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data SciencePaolo Missier
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxFIDO Alliance
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهMohamed Sweelam
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...FIDO Alliance
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...ScyllaDB
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfAnubhavMangla3
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 

Recently uploaded (20)

Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهله
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 

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].