using System.Collections.Generic;
using System.Threading.Tasks;
static public void Eat(Chopstick leftChopstick, Chopstick rightChopstick, int personNumber)
Console.WriteLine("Person {0} picked {1} chopstick.", personNumber, leftChopstick.ID);
Console.WriteLine("Person {0} picked {1} chopstick.", personNumber, rightChopstick.ID);
Console.WriteLine("Person {0} eats.", personNumber);
Console.WriteLine("Person {0} released {1} chopstick.", personNumber, rightChopstick.ID);
Console.WriteLine("Person {0} released {1} chopstick.", personNumber, leftChopstick.ID);
public int ID { get; set; }
public static void Main(string[] args)
var chopsticks = new List<Chopstick>();
for (int i = 0; i < numofChopsticks; i++)
chopsticks.Add(new Chopstick(i+1));
var tasks = new List<Task>();
for (int i = 0; i < numofPersons-1; i++)
tasks.Add(new Task(() => Person.Eat(chopsticks[0], chopsticks[numofPersons - 1], j + 1)));
tasks.Add(new Task(() => Person.Eat(chopsticks[j + 1], chopsticks[j], j + 1)));
Parallel.ForEach(tasks, t =>
Task.WaitAll(tasks.ToArray());