2013-03-05 31 views
0

我有一个问题已经由我们的讲师发出。它指的是等待进入环岛的汽车,使环岛不会堵塞。他曾经给了我们一个方法来解决这个问题,用“等待(B S)”语句,并要求我们将它转​​换为使用信号量,但是他给出的代码并不计算lol。我不知道他在玩什么的,但它没有道理在Java中....谁能帮我翻译这个....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; 
} 
} 
} 

在此先感谢您的帮助!

+0

这更像是一个伪代码。 “过程主要”是指设置课程的主要方法。 “proCss carProcess”表示方法“carProcess”的方法标题。您也可以在主要方法中看到对'carProcess'的调用。为了等待,你必须根据条件添加等待方法。希望能帮助到你。 – lovetostrike 2013-03-05 21:29:40

+0

感谢您的回复。你对伪代码的权利以及进一步鼓吹的结果只是希望我们对这些等待声明中的每一个使用普通的线程,正如你所说的那样。有趣的是,他甚至从来没有在课堂上解释过这个问题,或者曾经给过我们任何这样的问题,以便在实践中去做......我讨厌这个人!再次感谢。 – Shanyman 2013-03-06 07:22:10

回答

0

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

e.g

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

我希望大家都不需要此信息!永远!