考虑一只由50只猫组成的小丑。有5碗猫粮可以喂它们。每个碗具有喂食所述数量的猫的能力。没有两只猫可以在同一个碗里同时吃。一只猫只能从一个碗里吃。如果没有免费的碗,猫必须等到任何一个碗自由。猫可以定期来检查是否有一个碗是免费的。在C#中模拟上述情况。猫将使用线程表示。可以使用具有喂食能力大小的阵列来模仿碗。使用feed_cat方法模拟进食。当猫被喂食时,该方法使线程睡眠1000毫秒。编写一个main方法,创建名为“Thread_1”,“Thread_2”等所有50个线程。方法feed_cat被传递给线程。当前活动线程的名称将打印在控制台上。 bowl1 = 10,bowl2 = 15,bowl3 = 12,bowl4 = 14,bowl5 = 9
答案 0 :(得分:0)
public static Semaphore Bouncer { get; set; }
public static void Main(string[] args)
{
// Create the semaphore with 3 slots, where 3 are available.
Bouncer = new Semaphore(3, 3);
// Open the feedcat.
feedcat();
}
public static void feedcat()
{
for (int i = 1; i <= 60; i++)
{
if (i <= 10)
{
Thread thread = new Thread(new ParameterizedThreadStart(cat));
thread.Start(i);
}
else if(i>10 && i<=25)
{
Thread thread1 = new Thread(new ParameterizedThreadStart(cat1));
thread1.Start(i);
}
else if (i>25 && i<=37) {
Thread thread2 = new Thread(new ParameterizedThreadStart(cat2));
thread2.Start(i);
}
else if (i > 37 && i<=51)
{
Thread thread3 = new Thread(new ParameterizedThreadStart(cat3));
thread3.Start(i);
}
else
{
Thread thread4 = new Thread(new ParameterizedThreadStart(cat4));
thread4.Start(i);
}
}
}
public static void cat(object args)
{
// Wait to enter the Bowl (a semaphore to be released).
Console.WriteLine("cat {0} is waiting to entering the Bowl.", args);
Bouncer.WaitOne();
Console.WriteLine("cat {0} is eating.", args);
Thread.Sleep(1000);
// Let one cat out (release one semaphore).
Console.WriteLine("cat {0} is leaving the Bowl1.", args);
Bouncer.Release(1);
//Console.WriteLine("Please press Enter Key for Move On");
}
public static void cat1(object args)
{
// Wait to enter the Bowl (a semaphore to be released).
Console.WriteLine("Cat {0} is waiting to entering the Bowl.", args);
Bouncer.WaitOne();
Console.WriteLine("cat {0} is eating.", args);
Thread.Sleep(1000);
// Let one cat out (release one semaphore).
Console.WriteLine("cat {0} is leaving the Bowl2.", args);
Bouncer.Release(1);
//Console.WriteLine("Please press Enter Key for Move On");
}
public static void cat2(object args)
{
// Wait to enter the Bowl (a semaphore to be released).
Console.WriteLine("Cat {0} is waiting to entering the Bowl.", args);
Bouncer.WaitOne();
Console.WriteLine("cat {0} is eating.", args);
Thread.Sleep(1000);
// Let one cat out (release one semaphore).
Console.WriteLine("cat {0} is leaving the Bowl3.", args);
Bouncer.Release(1);
// Console.WriteLine("Please press Enter Key for Move On");
}
public static void cat3(object args)
{
// Wait to enter the Bowl (a semaphore to be released).
Console.WriteLine("Cat {0} is waiting to entering the Bowl.", args);
Bouncer.WaitOne();
Console.WriteLine("cat {0} is eating.", args);
Thread.Sleep(1000);
// Let one cat out (release one semaphore).
Console.WriteLine("cat {0} is leaving the Bowl4.", args);
Bouncer.Release(1);
// Console.WriteLine("Please press Enter Key for Move On");
}
public static void cat4(object args)
{
// Wait to enter the Bowl (a semaphore to be released).
Console.WriteLine("Cat {0} is waiting to entering the Bowl.", args);
Bouncer.WaitOne();
Console.WriteLine("cat {0} is eating.", args);
Thread.Sleep(1000);
// Let one cat out (release one semaphore).
Console.WriteLine("cat {0} is leaving the Bowl5.", args);
Bouncer.Release(1);
// Console.WriteLine("Please press Enter Key for Move On");
}
}