Sleeping Barber Problem | Oracle Java Combo
The Sleeping-Barber Problem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.
Good Java code:
https://github.com/MikeChristianson/sleeping-barber/tree/master/src/name/christianson/mike
https://vyatkins.wordpress.com/2013/12/21/sleeping-barber-problem/
X. Using Block Queue
You can easily modify this code for example if you need to add new barber like this
Actors in Action – Sleeping Barber Problem with AKKA
Sleeping barber problem using Semaphores
How to use Kafka message broker for Dijkstra’s Sleeping Barber problem
Read full article from Sleeping Barber Problem | Oracle Java Combo
The Sleeping-Barber Problem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.
Good Java code:
https://github.com/MikeChristianson/sleeping-barber/tree/master/src/name/christianson/mike
private final static AtomicBoolean shopOpen = new AtomicBoolean(); private final static AtomicInteger totalHaircuts = new AtomicInteger(); | |
private final static AtomicInteger lostCustomers = new AtomicInteger(); | |
private final BlockingQueue<Object> waitingRoom = new LinkedBlockingQueue<>(NUM_WAITING_ROOM_CHAIRS); |
https://vyatkins.wordpress.com/2013/12/21/sleeping-barber-problem/
X. Using Block Queue
public
class
BlockinQueueSleepingBarber
extends
Thread {
public
static
final
int
CHAIRS =
5
;
public
static
final
long
BARBER_TIME =
5000
;
private
static
final
long
CUSTOMER_TIME =
2000
;
public
static
final
long
OFFICE_CLOSE = BARBER_TIME *
2
;
public
static
BlockingQueue queue =
new
ArrayBlockingQueue(CHAIRS);
class
Customer
extends
Thread {
int
iD;
boolean
notCut =
true
;
BlockingQueue queue =
null
;
public
Customer(
int
i, BlockingQueue queue) {
iD = i;
this
.queue = queue;
}
public
void
run() {
while
(
true
) {
// as long as the customer is not cut he is in the queue or if not enough sits he is out
try
{
this
.queue.add(
this
.iD);
this
.getHaircut();
// take a sit
}
catch
(IllegalStateException e) {
System.out.println("There are no free seats. Customer "
+
this
.iD + " has left the barbershop.");
}
break
;
}
}
// take a seat
public
void
getHaircut() {
System.out.println("Customer " +
this
.iD + " took a chair");
}
}
class
Barber
extends
Thread {
BlockingQueue queue =
null
;
public
Barber(BlockingQueue queue) {
this
.queue = queue;
}
public
void
run() {
while
(
true
) {
// runs in an infinite loop
try
{
Integer i =
this
.queue.poll(OFFICE_CLOSE, TimeUnit.MILLISECONDS);
if
(i==
null
)
break
;
// barber slept for long time (OFFICE_CLOSE) no more clients in the queue - close office
this
.cutHair(i);
// cutting...
}
catch
(InterruptedException e) {
}
}
}
// simulate cutting hair
public
void
cutHair(Integer i) {
System.out.println("The barber is cutting hair
for
customer #" + i);
try
{
sleep(BARBER_TIME);
}
catch
(InterruptedException ex) {
}
}
}
public
static
void
main(String args[]) {
BlockinQueueSleepingBarber barberShop =
new
BlockinQueueSleepingBarber();
barberShop.start();
// Let the simulation begin
}
public
void
run() {
Barber giovanni =
new
Barber(BlockinQueueSleepingBarber.queue);
giovanni.start();
// create new customers
for
(
int
i =
1
; i <
16
; i++) {
Customer aCustomer =
new
Customer(i, BlockinQueueSleepingBarber.queue);
aCustomer.start();
try
{
sleep(CUSTOMER_TIME);
}
catch
(InterruptedException ex) {};
}
}
}
Barber barber01 =
new
Barber(BlockinQueueSleepingBarber.queue, "
01
");
barber01.start();
Barber barber02 =
new
Barber(BlockinQueueSleepingBarber.queue, "
02
");
barber02.start();
Actors in Action – Sleeping Barber Problem with AKKA
public class Barber implements Runnable {
Queue<Customer>customers;
private Shop shop;
public Barber(Shop shop) {
this.shop = shop;
customers = new LinkedBlockingQueue & lt; Customer & gt;
();
}
public void addCustomerToQueue(Customer customer) {
customers.add(customer);
}
public void run() { // add while(running)
while (!customers.isEmpty()) {
Customer customer = customers.remove();
performHaircut(customer);
shop.releaseChair();
} // ===> }
}
private void performHaircut(Customer customer) {
System.out.println("Customer " + customer.getId()
+ " has got a haircut");
}
}
public class Shop {
private Barber barber;
private Semaphore semaphore;
private AtomicInteger count = new AtomicInteger(0);
public Shop() {
semaphore = new Semaphore(3);
}
public void init() {
barber = new Barber(this);
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(barber, 100,
1000, TimeUnit.MILLISECONDS);
}
public void acquireChair(Customer customer) throws InterruptedException {
boolean acquired = semaphore.tryAcquire(1, 100, TimeUnit.MILLISECONDS);
if (acquired) {
count.getAndIncrement();
barber.addCustomerToQueue(customer);
return;
}
System.out.println("Turning customer " + customer.getId() + " away");
}
public void releaseChair() {
semaphore.release(1);
}
public int getSuccessfulHaircutCount() {
return count.intValue();
}
}
AKKA:public class Barber extends UntypedActor {
Random random = new Random();
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof Customer) {
Customer customer = (Customer) message;
Thread.sleep(random.nextInt(1150));
System.out.println("Customer " + customer.getId() + " Got a Haircut");
}
}
}
Sleeping barber problem using Reentrant Lockspublic class Shop extends UntypedActor { Queue<Customer>customers=new LinkedBlockingQueue<Customer>(); ActorRef barber; int counter = 0; public Shop(ActorRef barber) { this.barber = barber; } @Override public void onReceive(Object message) throws Exception { if (message instanceof Customer) { processCustomer(message); } else if (message instanceof WakeUp) { barber.tell(customers.poll()); } } private void processCustomer(Object message) { if (customers.size() == 3) { Customer customer = (Customer) message; System.out.println("Customer " + customer.getId() + " is leaving"); return; } counter++; customers.add((Customer) message); } @Override public void postStop() { System.out.println(counter + " Customers got a haircut today"); super.postStop(); } }
Sleeping barber problem using Semaphores
How to use Kafka message broker for Dijkstra’s Sleeping Barber problem
Read full article from Sleeping Barber Problem | Oracle Java Combo