Skip to content

Instantly share code, notes, and snippets.

@chathurawidanage
Last active February 28, 2017 18:56
Show Gist options
  • Save chathurawidanage/8effb3b57240655a1db25fb970707fac to your computer and use it in GitHub Desktop.
Save chathurawidanage/8effb3b57240655a1db25fb970707fac to your computer and use it in GitHub Desktop.
import java.util.concurrent.Semaphore;
/**
* @author Chathura Widanage
*/
public class Pizza {
private static volatile Semaphore pizzaPlate = new Semaphore(1);
private static volatile Semaphore delivery = new Semaphore(1);
private static volatile boolean orderInProgress = true;//order is in progress for first pizza
private static volatile int pizzas = 0;//no pizza initially
private static volatile Object sleepObject = new Object();
public static void main(String[] args) throws InterruptedException {
//students
for (int i = 0; i < 6; i++) {
new Thread(new Runnable() {
public void run() {
while (true) {
boolean consumed = false;
try {
pizzaPlate.acquire();
System.out.println(String.format("%d checking plate", Thread.currentThread().getId()));
if (pizzas > 0) {
System.out.println(String.format("Grabbing one piece of pizza by %d", Thread.currentThread().getId()));
pizzas--;
consumed = true;
} else {
if (!orderInProgress) {
orderInProgress = true;
orderPizza();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
pizzaPlate.release();
}
if (!consumed) {//goto sleep if no pizza
synchronized (sleepObject) {
try {
System.out.println(String.format("No pizza in plate. %d is sleeping", Thread.currentThread().getId()));
sleepObject.wait();//student going to sleep
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
eatAndStudy();
}
}
}
}).start();
}
//Pizza delivery thread
new Thread(new Runnable() {
public void run() {
while (true) {
try {
delivery.acquire();
Thread.sleep(5000);//delivery delay
pizzaPlate.acquire();
pizzas = 10;//filling the plate with pizza
orderInProgress = false;
System.out.println("Pizza delivered");
synchronized (sleepObject) {
System.out.println("Waking up students");
sleepObject.notifyAll();//waking up students
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {//in any case release the pizza plate
pizzaPlate.release();
}
}
}
}).start();
}
public static void eatAndStudy() {
try {
System.out.println(String.format("%d eating and studying", Thread.currentThread().getId()));
Thread.sleep(5000);//studying
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void orderPizza() {
System.out.println("Order pizza by " + Thread.currentThread().getId());
delivery.release();
}
}
@chathurawidanage
Copy link
Author

A group of students are studying for CS 4532 exam. The students can study only while eating pizza.
Each student executes the following loop:

while (true) {
 pick up a slice of pizza;
 study while eating the pizza
}

If a student finds that the pizza is gone, the student goes to sleep until another pizza arrives. The first
student to discover that the group is out of pizza calls Kamal’s Pizza to order another pizza before
going to sleep. Each pizza has s slices. Once Kamal delivers pizza, he wake up all the students in the
group. Then the students pick up a slice of pizza and go back to studying, and the process continues.
Write code to synchronize the student threads and the Kamal’s pizza delivery thread.
Your solution should avoid deadlock and call Kamal’s Pizza (i.e., wake up the delivery thread) exactly
once each time a pizza is exhausted. No slice/piece of pizza may be consumed by more than one
student

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment