我是C#和线程方面的新手。
为了熟悉Monitor.Wait、Monitor.lock和Monitor.PulseAll,我描述了下面的情况:
“不同的团队共享一个足球场地进行训练。同时只有一个团队能够使用场地进行训练。一个团队可以使用场地进行30分钟的训练。当时间到达25分钟时,它应该向其他线程发出信号,告知场地将在5分钟后释放。当场地潮湿(枚举有三个值:free,allotted,wet)时,所有团队都不能锁定场地,必须等待10分钟。”
老实说,我不知道如何将描述转换为实际的代码。根据我的理解,我设计了大纲。
namespace ThreadingSimulation
{
// A Random Activity can be picked up from this enum by a team
public enum RandomGroundStatus
{
free,
allotted,
Wet
}
class FootBallGround
{
public void Playing(object obj)
{
// Here the name of the team which is using the ground will be printed
// Once the time is reached to 25 minnutes the active thread acquired
// the lock will signal other threads
}
public void GroundCleaningInProgress(object obj)
{
// Ground cleaning is in progress all of you
// wait for 10 minutes
}
}
class Team
{
string teamName;
static void Main()
{
//select random value for GrandStatus from enum
// if the ground is wet no team is allowed to get the
// ground for 10 minutes
//if the ground is free "Team A" locks the ground
// otherwise "Team B" locks the ground
}
}
}
我不知道如何应用锁和信号,请帮助我。