A little push using Semaphones
Let me first explain the project: I have 1 chef 3 homers, (Producer-Consumer problem). The chef has an unlimited supply of 3 ingredients. Each homer has 1 of the 3 ingredients in unlimited supply, but needs 2 of the other ingredients to complete the donut. Each homer can take 2 of the ingredients he needs to complete the donut. I must use semaphores to complete the assignment.
This is the code I have come up with, but I am unable to get much further. Maybe someone can push me in the right direction.
Code:
//package donutchef;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;
import java.util.List;
/**
*
* @author Telamon
*/
public class Donut
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
Homer h1 = Homer.getInstance("Homer 1");
Homer h2 = Homer.getInstance("Homer 2");
Homer h3 = Homer.getInstance("Homer 3");
DonutChef dc1 = DonutChef.getInstance();
Table table1 = Table.getInstance();
table1.addCustomer(h1);
table1.addCustomer(h2);
table1.addCustomer(h3);
table1.addChef(dc1);
Thread Homer1 = new Thread(h1);
Thread Homer2 = new Thread(h2);
Thread Homer3 = new Thread(h3);
Thread DonutChef1 = new Thread(dc1);
}
}
class DonutChef implements Runnable
{
private DonutChef()
{
}
public static DonutChef getInstance()
{
return new DonutChef();
}
public void notifyChef(){
}
public void run()
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
class Homer implements Runnable
{
private String name;
private Homer ()
{
}
private Homer (String name) {
this.name = name;
}
public static Homer getInstance(String name){
return new Homer(name);
}
private void makeDonut()
{
}
private void eatDonut()
{
}
public void run() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
class Table {
private static Semaphore dsHomer = new Semaphore(0);
private static Semaphore fillingHomer = new Semaphore(0);
private static Semaphore sprinklesHomer = new Semaphore(0);
private List<Homer> Customers;
private List<DonutChef> Chefs;
private Table(){
Customers = new LinkedList<Homer>();
Chefs = new LinkedList<DonutChef>();
}
public static Table getInstance(){
return new Table();
}
public void addCustomer(Homer h){
Customers.add(h);
}
public void addChef(DonutChef donutChef){
Chefs.add(donutChef);
}
}