using System.Collections.Generic;
using System.Threading.Tasks;
public static void Main()
var chopStick = new Dictionary<int, object>(persons);
for (int i = 0; i < persons; ++i)
chopStick.Add(i, new object());
Task[] task = new Task[persons];
task[0] = new Task(() => Person.Eat(chopStick[0], chopStick[persons - 1], 1, 1, persons));
for (int i = 1; i < persons; ++i)
task[x] = new Task(() => Person.Eat(chopStick[x - 1], chopStick[x], x + 1, x, x + 1));
Parallel.ForEach(task, t =>
Console.WriteLine("Done - all {0} persons", persons);
public static void Eat(object leftChopStick, object rightChopstick, int person, int leftChopstickNum, int rightChopstickNum)
Console.WriteLine("Person {0} picked {1} chopstick ", person, leftChopstickNum);
Console.WriteLine("Person {0} picked {1} chopstick ", person, rightChopstickNum);
Console.WriteLine("Person {0} eat", person);
Console.WriteLine("Person {0} released {1} chopstick", person, rightChopstickNum);
Console.WriteLine("Person {0} released {1} chopstick", person, leftChopstickNum);