using System.Threading.Tasks;
public Person(Chopstick l, Chopstick r, int n)
Console.WriteLine(" person {0} picked {1} chopstick.", this.number, this.left.numberC);
Console.WriteLine(" person {0} picked {1} chopstick.", this.number, this.right.numberC);
Console.WriteLine("person {0} eats.", this.number);
Console.WriteLine(" person {0} released {1} chopstick.", this.number, this.right.numberC);
Console.WriteLine(" person {0} released {1} chopstick.", this.number, this.left.numberC);
public static void Main()
const int numPersons = 5;
Chopstick[] chopsticks = new Chopstick[numPersons];
for (int i = 0; i < numPersons; i++)
chopsticks[i] = new Chopstick(i + 1);
Task[] tasks = new Task[numPersons];
for (int i = 0; i < numPersons; i++)
Person person = new Person(chopsticks[i], chopsticks[personNum % numPersons], personNum);
tasks[i] = new Task(() =>
Parallel.ForEach(tasks, t =>
Console.WriteLine("\n Done - all 5 people have eaten.");