public void EatWith(Spoon spoon, Diner spouse) { while (IsHungry) { // Don't have the spoon, so wait patiently for spouse. if (spoon.Owner != this) { try { Thread.Sleep(1); } catch (ThreadInterruptedException) { } continue; } // If spouse is hungry, insist upon passing the spoon. if (spouse.IsHungry) { Console.WriteLine("{0}: You eat first my darling {1}!", Name, spouse.Name); spoon.SetOwner(spouse); continue; } // Spouse wasn't hungry, so finally eat spoon.Use(); IsHungry = false; Console.WriteLine("{0}: I am stuffed, my darling {1}!", Name, spouse.Name); spoon.SetOwner(spouse); } }
public static void Main(string[] args) { var husband = new Diner("Bob"); var wife = new Diner("Alice"); var s = new Spoon(husband); Task.WaitAll( Task.Run(() => husband.EatWith(s, wife)), Task.Run(() => wife.EatWith(s, husband)) ); }
public void SetOwner(Diner d) { Owner = d; }
public Spoon(Diner diner) { Owner = diner; }