简体   繁体   中英

Java Await statement - Concurrent programming

I have a question that has been given out by our lecturer. It refers to cars waiting to get onto a roundabout so that the roundabout does not clog up. He has supposedly given us a method to solve this using "await( BS)" statements and asked us to convert it to using semaphores, however the code he has given does not compute lol. I dont know what he was playing at but it doesnt make sense in Java.... can anyone help me translate this....

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;
}
}
}

Thanks in advance for any help!

What he eventually told us is that the question he gave us was from years ago. The code in the question is redundent Java. For anybody else with a douce of a teacher, when this format of code comes up "await((numberOnRoundabout<=6), numberOnRoundabout++)" it is old Java. Translated it will be a series of thread objects created with the conditions set within them.

eg

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

I do hope nobody needs this information! Ever!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM