2013-03-05 56 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 

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