Skip to content

Instantly share code, notes, and snippets.

@Vini2
Last active June 19, 2017 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vini2/1ba4b9cc15222b81d5c50aeb11e200c4 to your computer and use it in GitHub Desktop.
Save Vini2/1ba4b9cc15222b81d5c50aeb11e200c4 to your computer and use it in GitHub Desktop.
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import static java.lang.Thread.sleep;
import java.util.Random;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vijini
*/
public class PizzaProblem {
public static void main(String[] args) {
StudyGroup studyGroup = new StudyGroup(); //Create student group
int students = 6;
for (int i = 0; i < students; i++) {
new Student(studyGroup).start(); //Create Student threads
}
new PizzaDelivery(studyGroup).start(); //Create Pizza Delivery thread
}
}
//Study group class
class StudyGroup {
int S = 5;
int slices = 0; //No. of pizza slices
private final Lock mutex = new ReentrantLock(); //Lock to ensure one Student can take a slice at a time
private final Condition orderPizza = mutex.newCondition(); //Condition to order pizza
private final Condition deliverPizza = mutex.newCondition(); //Condition to deliver pizza
private boolean firstToSee = true; //First student to see group is out of pizza
void beginStudying() throws InterruptedException {
mutex.lock();
if (slices > 0) {
--slices; //Decrease no. of slices. Student picks up a slice of pizza.
//Student takes a slice and starts studying
System.out.println("Student " + Thread.currentThread().getId() + " took a slice of pizza and is studying");
} else {
if (firstToSee) {
//First student to see that the group is out of pizza
System.out.println("Group out of pizza. Student " + Thread.currentThread().getId() + " calls Kamal's Pizza");
orderPizza.signal(); //Call Kamal's Pizza and wake up delivery thread
firstToSee = false;
}
System.out.println("Student " + Thread.currentThread().getId() + " sleeps");
deliverPizza.await(); //Student sleeps till pizza is delivered
}
mutex.unlock();
}
void checkOrder() throws InterruptedException {
mutex.lock();
slices = S; //Fill plate with new pizza
System.out.println("Pizza delivered");
firstToSee = true;
System.out.println("Wake up sleeping students\n");
deliverPizza.signalAll(); //Wake up all sleeping Students
orderPizza.await(); //PizzaDelivery goes to sleep
mutex.unlock();
}
}
//Pizza Delivery class
class PizzaDelivery extends Thread{
private StudyGroup studyGroup = new StudyGroup();
public PizzaDelivery(StudyGroup sg) {
this.studyGroup = sg;
}
@Override
public void run(){
while (true) {
try {
studyGroup.checkOrder();
sleep(5000); //Wait till pizza is delivered
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
//Student class
class Student extends Thread{
StudyGroup studyGroup = new StudyGroup();
private Random r = new Random();
public Student(StudyGroup sg) {
this.studyGroup = sg;
}
@Override
public void run(){
while (true) {
try {
studyGroup.beginStudying();
sleep(r.nextInt(10000)); //Wait till pizza slice finishes
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
@Vini2
Copy link
Author

Vini2 commented Jun 19, 2017

Problem Statement:
A group of students are studying for an 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. Comment your code.

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