对不起,我的英语不好,非常感谢您的帮助。
我有一个开始我的任务的班级:
Report report = new Report();
//Fork to left of Plato and right of Galileo.
Fork fork1 = new Fork(1, report);
fork1.m_forkMaterial = fork1.GetRandomMaterial();
//Fork to left of Confucius and right of Plato.
Fork fork2 = new Fork(2, report);
fork2.m_forkMaterial = fork1.GetRandomMaterial();
//Fork to left of Aristotle and right of Confucius.
Fork fork3 = new Fork(3, report);
fork3.m_forkMaterial = fork1.GetRandomMaterial();
//Fork to left of Socrates and right of Aristotle.
Fork fork4 = new Fork(4, report);
fork4.m_forkMaterial = fork1.GetRandomMaterial();
//Fork to right of Socrates and left of Galileo.
Fork fork5 = new Fork(5, report);
fork5.m_forkMaterial = fork1.GetRandomMaterial();
Task platoTask = new Task(() => new Philosopher.Philosopher("Plato", report, fork1, fork2));
Task confuciusTask = new Task(() => new Philosopher.Philosopher("Confucius", report, fork2, fork3));
Task aristotleTask = new Task(() => new Philosopher.Philosopher("Aristotle", report, fork3, fork4));
Task socratesTask = new Task(() => new Philosopher.Philosopher("Socrates", report, fork4, fork5));
Task galileoTask = new Task(() => new Philosopher.Philosopher("Galileo", report, fork5, fork1));
platoTask.Start();
confuciusTask.Start();
aristotleTask.Start();
socratesTask.Start();
galileoTask.Start();
现在我有一个大问题...我在任务和多线程方面工作很少...
问题:我需要按照将要传递给方法(或构造函数)的顺序执行任务 像这样:
College college = new College(1,2,3,4,5); - it's my seq for start tasks.
我该怎么做?谢谢你的帮助。
更新: 我的哲学家课:
private Random rng = new Random();
private Report reportObject;
private Fork leftFork;
private Fork rightFork;
private string Name { get; set; }
public string Status { get; private set; }
public Philosopher(string name, Report report, Fork leftFork, Fork rightFork)
{
this.Name = name;
this.reportObject = report;
this.leftFork = leftFork;
this.rightFork = rightFork;
StateTransition();
}
public void StateTransition()
{
while (true)
{
EnterThinkingState();
Thread.Sleep(3000);
EnterHungryState();
}
}
public void EnterThinkingState()
{
Thread.Sleep(rng.Next(1000) + 1);
Status = "thinking";
reportObject.ReceiveMessage(Name + " is " + Status);
Thread.Sleep(rng.Next(1000) + 1);
}
public void EnterHungryState()
{
Status = "hungry";
reportObject.ReceiveMessage(Name + " is " + Status);
bool leftForkTaken = false;
bool rightForkTaken = false;
while (!leftForkTaken && !rightForkTaken)
{
if (!leftForkTaken)
{
Monitor.TryEnter(leftFork, 1000, ref leftForkTaken);
if (leftForkTaken)
{
leftFork.ForkAction(Name, Fork.ForkActionTypes.kPickUp);
}
}
if (!rightForkTaken)
{
Monitor.TryEnter(rightFork, 1000, ref rightForkTaken);
if (rightForkTaken)
{
rightFork.ForkAction(Name, Fork.ForkActionTypes.kPickUp);
}
}
}
EnterEatingState();
Monitor.Exit(leftFork);
Monitor.Exit(rightFork);
}
public void EnterEatingState()
{
Status = "eating";
reportObject.ReceiveMessage(Name + " is " + Status);
Thread.Sleep(rng.Next(1000) + 1);
leftFork.ForkAction(Name, Fork.ForkActionTypes.kPutDown);
rightFork.ForkAction(Name, Fork.ForkActionTypes.kPutDown);
}
我的叉子课:
public class Fork
{
private int id;
private readonly Report _reportObject;
public enum ForkMaterial
{
kSilver = 0,
kGold = 1
}
public enum ForkActionTypes
{
kPickUp = 0,
kPutDown = 1
}
public ForkMaterial m_forkMaterial { get; set; }
public Fork(int id, Report report)
{
this.id = id;
this._reportObject = report;
}
public void ForkAction(string name, ForkActionTypes action)
{
String actionName = (action == ForkActionTypes.kPickUp) ? " has been picked up by " : " has been put down by ";
String materialName = (m_forkMaterial == ForkMaterial.kGold) ? "Gold " : "Silver ";
_reportObject.ReceiveMessage(materialName + "Fork " + id + actionName + name);
}
public ForkMaterial GetRandomMaterial()
{
Random random = new Random(Guid.NewGuid().GetHashCode());
int materialNumber = random.Next(100);
if (materialNumber <= 50)
{
return ForkMaterial.kGold;
}
else
{
return ForkMaterial.kSilver;
}
}
}