所以我对下面的餐饮哲学家的问题有一个基本的解决方案:
using System;
using System.Threading.Tasks;
namespace Conductor
{
class Program
{
private static object _locker = new object();
static void Main(string[] args)
{
object[] sticks = {new object(),
new object(),
new object(),
new object(),
new object()};
for (int i = 0; i<5; i++)
{
int next = (i + 1) % 5;
Task.Factory.StartNew(() => Eat(sticks[i], sticks[next], i));
}
}
static void Eat(object _leftChopstick, object _rightChopstick, int i)
{
lock (_locker)
{
lock (_leftChopstick)
{
lock (_rightChopstick)
{
Console.WriteLine("Eating" + i);
}
}
}
}
}
}
虽然它可能不是最有效的解决方案,但它会在&#34; StartNew&#34;上引发IndexOutOfRange异常。当我被设置为5时调用。我无法弄清楚如何使用任务工厂以某种方式使我的程序忽略for循环条件。
如果我运行此代码,那很好:
using System;
using System.Threading.Tasks;
namespace Conductor
{
class Program
{
private static object _locker = new object();
static void Main(string[] args)
{
object[] sticks = { new object(),
new object(),
new object(),
new object(),
new object()};
int i = 0;
Task.Factory.StartNew(() => Eat(sticks[i], sticks[i + 1], i));
Task.Factory.StartNew(() => Eat(sticks[i+1], sticks[i + 2], i + 1));
Task.Factory.StartNew(() => Eat(sticks[i+2], sticks[i + 3], i + 2));
Task.Factory.StartNew(() => Eat(sticks[i+3], sticks[i + 4], i + 3));
Task.Factory.StartNew(() => Eat(sticks[i + 4], sticks[0], i + 4));
Console.ReadLine();
}
static void Eat(object _leftChopstick, object _rightChopstick, int i)
{
lock (_locker)
{
lock (_leftChopstick)
{
lock (_rightChopstick)
{
Console.WriteLine("Eating" + i);
}
}
}
}
}
}
另外,如果我通过循环调试,我得到相同的异常。
对此的任何帮助将不胜感激。对不起,如果它太基本或者我的代码中有一些微不足道的缺陷我就错过了。感谢。
答案 0 :(得分:0)
for (int i = 0; i < 5; i++)
{
int j = i;
int next = (i + 1) % 5;
Task.Factory.StartNew(() => Eat(sticks[j], sticks[next], j));
}