using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApplication139
public sealed class persons
static public void eat(object leftchopstic, object rightchopstic, int numofpersons, int noleftchopstic, int norightchopstic)
Console.WriteLine("person number {0} picked chopstic number {1}", numofpersons, noleftchopstic);
Console.WriteLine("person number {0} picked chopstic number {1}", numofpersons, norightchopstic);
Console.WriteLine("person number {0} eat", numofpersons);
Console.WriteLine("person number {0} release chopstic number {1}", numofpersons, norightchopstic);
Console.WriteLine("person number {0} release chopstic number {1}", numofpersons, noleftchopstic);
const int numofpersons = 5;
var chopsticks = new Dictionary<int, object>(numofpersons);
for (int i=0; i < numofpersons; ++i)
chopsticks.Add(i, new object());
Task[] task = new Task[numofpersons];
task[0] = new Task(() => persons.eat(chopsticks[0], chopsticks[numofpersons - 1], 0 + 1, 1, numofpersons));
for (int i=1; i < numofpersons; ++i)
task[ix] = new Task(() => persons.eat(chopsticks[ix - 1], chopsticks[ix], ix + 1, ix, ix + 1));
Parallel.ForEach(task, t =>
Console.WriteLine("=======================================================================");
Console.WriteLine("Done - all 5 people have eaten.");