0

我有一个问题是我们的讲师提出的。它是指等待上环岛的汽车,以免环岛堵塞。据说他给了我们一种使用“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;
}
}
}

提前感谢您的帮助!

4

1 回答 1

1

他最终告诉我们的是,他给我们的问题是多年前的问题。问题中的代码是冗余的 Java。对于其他任何有老师的人来说,当这种代码格式出现“await((numberOnRoundabout<=6), numberOnRoundabout++)”时,它就是旧的 Java。翻译过来的将是一系列使用其中设置的条件创建的线程对象。

例如

public synchronized void myStop() {
      this.runningFlag=false;
      t.interrupt();
      } // end myStart

我希望没有人需要这些信息!曾经!

于 2013-03-06T21:10:04.200 回答