我有一个问题是我们的讲师给出的。它指的是等待上环形交叉路口的汽车,这样环形交叉路口就不会堵塞。据推测,他给了我们一个使用"await( B,S)“语句来解决这个问题的方法,并要求我们将其转换为使用信号量,但是他给出的代码并不计算lol。我不知道他在玩什么,但这在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;
}
}
}提前感谢您的帮助!
发布于 2013-03-07 05:10:04
他最终告诉我们的是,他给我们的问题是多年前的事了。问题中的代码是冗余Java。对于任何其他有一个愚蠢的老师的人来说,当这种格式的代码出现"await((numberOnRoundabout<=6),numberOnRoundabout++)“时,它就是旧的Java。翻译过来,它将是一系列使用其中设置的条件创建的线程对象。
e.g
public synchronized void myStop() {
this.runningFlag=false;
t.interrupt();
} // end myStart我真的希望没有人需要这些信息!永远不会!
https://stackoverflow.com/questions/15233444
复制相似问题