我有一个问题是我们的讲师提出的。它是指等待上环岛的汽车,以免环岛堵塞。据说他给了我们一种使用“await(BS)”语句来解决这个问题的方法,并要求我们将其转换为使用信号量,但是他给出的代码不能计算大声笑。我不知道他在玩什么,但这在 Java 中没有意义……谁能帮我翻译一下……
package roundabout;
public class roundabout01
{
Process main;
{
/* declare and initialize global variables */
int NUMBER_OF_CARS = 20;
int numberOnRoundabout = 0;
/* slots on the roundabout */
String slots [] = {"[.....]", "[.....]", "[.....]", "[.....]", "[.....]", "[.....]"};
/* create and set the cars moving */
for (int count = 1; NUMBER_OF_CARS < 6; count++);
{
int entry = 3;
int exit = 3;
carProcess(entry, exit);
// end for; /*end main process*/
}
Process carProcess (int s, int t);
{
<await((numberOnRoundabout<=6), numberOnRoundabout++)>; /* Wait if roundabout crowded*/
/* wait for clearance before moving on to the roundabout */
<await(slots[2..s]=="[.....]"); AND (slots[2..s + 7] mod 8 == "[.....]");
slots[2..s]="["+entry+"-->"+exit+"]";
int currentPosition = 2*s;
int nextPosition = 2*s+1; /* move around to exit position (which is 2t) */
}
do
{
<await(slots[nextPosition]="[.....]");
slots[nextPosition]=slots[currentPosition];
slots[currentPosition]="[.....]">
currentPosition != nextPosition;
nextPosition = (nextPosition + 1) mod 8;
}
while (currentPosition != 2*t);
{
slots[currentPosition]="[.....]";
numberOnRoundabout -- ; /* move off the roundabout */
End carProcess;
}
}
}
提前感谢您的帮助!