using System.Collections.Generic;
using System.Threading.Tasks;
namespace Multithreading_Assignment_3
public static void Main(string[] args)
const int num_of_Persons = 5;
var chopsticks = new Dictionary<int, object>(num_of_Persons);
for (int i = 0; i < num_of_Persons; ++i)
chopsticks.Add(i, new object());
Task[] tasks = new Task[num_of_Persons];
tasks[0] = new Task(() => Person.Eat(chopsticks[0], chopsticks[num_of_Persons - 1], 0 + 1, 1, num_of_Persons));
for (int i = 1; i < num_of_Persons; ++i)
tasks[x] = new Task(() => Person.Eat(chopsticks[x - 1], chopsticks[x], x + 1, x, x + 1));
Parallel.ForEach(tasks, t =>
public static void Eat(object leftChopstick, object rightChopstick, int personNumber, int leftChopstickNumber, int rightChopstickNumber)
Console.WriteLine(" Person {0} picked {1} chopstick.", personNumber, leftChopstickNumber);
Console.WriteLine(" Person {0} picked {1} chopstick.", personNumber, rightChopstickNumber);
Console.WriteLine("---------------------------------Person {0} eats.", personNumber);
Console.WriteLine(" Person {0} released {1} chopstick.", personNumber, rightChopstickNumber);
Console.WriteLine(" Person {0} released {1} chopstick.", personNumber, leftChopstickNumber);
public static void complete()
Console.WriteLine("All 5 people have eaten");