'understanding solution to sleeping barber algorithm in java
I am trying to implement sleeping barber problem in java with this logic:
If customer comes he wakes up the sleeping barber. If he finds a chair in the waiting room he sits else he waits till it gets available. The barber return to sleep if there are no customers in the waiting room.
Note: There is only one chair in the room.
This is my java class shop the shared object between thread client and thread barber. If anyone needs the class barber and client for a better understanding I will send it to him.
I created 1 barber and 9 clients.
package barbier;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class Shop {
private static final int MAX_AVAILABLE = 1;
public static Semaphore place;
static List<Client> listCustomer;
public Shop() {
place=new Semaphore(MAX_AVAILABLE,true);
listCustomer = new LinkedList<Client>();
}
public void cutHair() {
Client client;
synchronized (listCustomer) {
while(listCustomer.size()==0) {
System.out.println("Barber is waiting for customer.");
try {
listCustomer.wait();
}
catch(InterruptedException iex) {
iex.printStackTrace();
}
}
System.out.println("Barber found a customer in the queue.");
client = (Client)((LinkedList<?>)listCustomer).poll();
place.release();
}
long duration=0;
try {
System.out.println("Cuting hair of Customer : "+client.getName());
duration = (long)(Math.random()*10);
TimeUnit.SECONDS.sleep(duration);
}
catch(InterruptedException iex) {
iex.printStackTrace();
}
System.out.println("Completed Cuting hair of Customer : "+client.getName() + " in "+duration+ " seconds.");
}
public static void add(Client client) throws InterruptedException {
System.out.println("Customer : "+client.getName()+ " entering the shop at "+client.getInTime());
synchronized (listCustomer) {
place.acquire();
((LinkedList<Client>)listCustomer).offer(client);
System.out.println("Customer : "+client.getName()+ " got the chair.");
if(listCustomer.size()==1)
listCustomer.notify();
}
}
}
My problem was when two clients enter the shop and one finds a chair while the other does not, the thread barber stops working.
This is an example:
Customer : Client 5 entering the shop at Sun Mar 13 21:24:53 CET 2022
Customer : Client 5 got the chair.
Barber found a customer in the queue.
Customer : Client 6 entering the shop at Sun Mar 13 21:24:55 CET 2022
Customer : Client 6 got the chair.
Cuting hair of Customer : Client 5
Customer : Client 7 entering the shop at Sun Mar 13 21:25:00 CET 2022
Completed Cuting hair of Customer : Client 5 in 6 seconds.
Customer : Client 8 entering the shop at Sun Mar 13 21:25:03 CET 2022
Customer : Client 9 entering the shop at Sun Mar 13 21:25:03 CET 2022
/*Nothing happens after that barber stops working!!!!*/
The problem is when client 6 and client 7 enter the shop, client 6 got a chair but client 7 doesn't as there is one chair. After that barber thread stop working despite there's a client in the waiting room (client 6 got a chair in the room) and that's weird. Later I find the solution but I don't have an explanation for it:
I just moved the place.aquire(); in the add() function out from the synchronized block and then everything works perfectly and barber thread continue working. My question is WHY is that happening? Please anyone can explain for me and thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
