Last active
June 19, 2017 12:16
-
-
Save Vini2/1ba4b9cc15222b81d5c50aeb11e200c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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.