【发布时间】:2009-10-14 20:15:38
【问题描述】:
我是 CSharp 和线程的新手。
为了熟悉 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
}
}
}
这里我不知道如何申请锁和信号。请帮助我。
【问题讨论】:
标签: c# multithreading