Finally getting into concurrent programming but it's not easy for the novice like me, after all this work I still cannot get a successful compilation!!!

A simple roundabout (traffic circle) is a circular junction of four streets where traffic flows in a one-way and one-lane circular stream around a central island, cars can enter, travel around and leave the roudabout. A car joining the roundabout must first make sure the slot ahead and the slot to the right of them is free. It then moves around until it is opposite the desired exit slot. It then moves off the roundabout unhindered.To avoid car crashes only one car at a time can be in any of the available slots. Cars can enter from any of the four directions and leave from any of the exits. Additionally to avoid overcrowding(and possible deadlock!) a maximum of six cars is allowed on to the roundabout at any time! tricky is it not? lol.

This is the result of a weeks work below!

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

}
}

This is the full list of errors, I've managed to get only a few of them!

Description Resource Path Location Type
Syntax error on token ",", ; expected roundabout01.java /concurrent systems/src/roundabout line 26 Java Problem
Syntax error on token ";", invalid Expression roundabout01.java /concurrent systems/src/roundabout line 29 Java Problem
Syntax error on token "(", ; expected roundabout01.java /concurrent systems/src/roundabout line 26 Java Problem
Syntax error on token ")", ; expected roundabout01.java /concurrent systems/src/roundabout line 26 Java Problem
Syntax error on token "<", delete this token roundabout01.java /concurrent systems/src/roundabout line 42 Java Problem
Syntax error on token "<", throw expected roundabout01.java /concurrent systems/src/roundabout line 29 Java Problem
Syntax error on token "=", != expected roundabout01.java /concurrent systems/src/roundabout line 44 Java Problem
Syntax error on token "MOD", , expected roundabout01.java /concurrent systems/src/roundabout line 33 Java Problem
Syntax error on token "MOD", invalid AssignmentOperator roundabout01.java /concurrent systems/src/roundabout line 45 Java Problem
The method carProcess(int, int) is undefined for the type roundabout01 roundabout01.java /concurrent systems/src/roundabout line 21 Java Problem

Finally getting into concurrent programming but it's not easy for the novice like me, after all this work I still cannot get a successful compilation!!!

A simple roundabout (traffic circle) is a circular junction of four streets where traffic flows in a one-way and one-lane circular stream around a central island, cars can enter, travel around and leave the roudabout. A car joining the roundabout must first make sure the slot ahead and the slot to the right of them is free. It then moves around until it is opposite the desired exit slot. It then moves off the roundabout unhindered.To avoid car crashes only one car at a time can be in any of the available slots. Cars can enter from any of the four directions and leave from any of the exits. Additionally to avoid overcrowding(and possible deadlock!) a maximum of six cars is allowed on to the roundabout at any time! tricky is it not? lol.

This is the result of a weeks work below!

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

}
}

This is the full list of errors, I've managed to get only a few of them!

Description Resource Path Location Type
Syntax error on token ",", ; expected roundabout01.java /concurrent systems/src/roundabout line 26 Java Problem
Syntax error on token ";", invalid Expression roundabout01.java /concurrent systems/src/roundabout line 29 Java Problem
Syntax error on token "(", ; expected roundabout01.java /concurrent systems/src/roundabout line 26 Java Problem
Syntax error on token ")", ; expected roundabout01.java /concurrent systems/src/roundabout line 26 Java Problem
Syntax error on token "<", delete this token roundabout01.java /concurrent systems/src/roundabout line 42 Java Problem
Syntax error on token "<", throw expected roundabout01.java /concurrent systems/src/roundabout line 29 Java Problem
Syntax error on token "=", != expected roundabout01.java /concurrent systems/src/roundabout line 44 Java Problem
Syntax error on token "MOD", , expected roundabout01.java /concurrent systems/src/roundabout line 33 Java Problem
Syntax error on token "MOD", invalid AssignmentOperator roundabout01.java /concurrent systems/src/roundabout line 45 Java Problem
The method carProcess(int, int) is undefined for the type roundabout01 roundabout01.java /concurrent systems/src/roundabout line 21 Java Problem

this only very slightly represents java code... I take the guess you are busy porting code form vb ? I would suggest writing one part at a time, because the code given in my view has many syntactical errors? a template for class with a main method and simple method and for loop in java is:

package roundabout;

public class roundabout01 {
    /*
     * declare and initialize global variables
     */

    public static void main(String[] args) {
        String msg = "hi";
        say(msg);

    }

    static void say(String msg) {
        for (int i = 0; i < 2; i++) {
            System.out.println(msg);
        }
    }
}

hope maybe it helps a bit, get one thing at a time correct rather then writing entire code that has many errors and is hard for another to adjust to correct

Edited 4 Years Ago by DavidKroukamp: n/a

o.k so I've got this far with it now, but for the life of me I can't successfully change the carProcess class from await design to semaphore design which controls the traffic going onto and leaving the roundabout, any hints or tips would be great, thanks.

package roundabout;

public class roundabout02
{
	int NUMBER_OF_CARS = 20;	//declare and initialize global variables
	int numberOnRoundabout = 0;
	
	    public static void main(String[] args)
	    {
	    	Semaphore[] roundabout = new Semaphore[8];
	    	carProcess[] cars = new carProcess[20];
	    			 
	    			 
	    		for(int i = 0; i < 19; i++)
	    			 {
	    			 cars[i] = new carProcess();
	    			 cars[i].start(); // starts the car thread
	    			 }
	    }

	 // carProcess.java
	class carProcess
	{
	     private  Semaphore pmutex;   	// reference to shared semaphore

	     public void run()              // start carProcess
	    	 {
	         int t;

	         for (t = 0; t < 6; t++) 
	             {
	             pmutex.P();
	             
	             // critical area (not complete)
	             
	             pmutex.V();
	             }
	          System.out.println("carProcess finished");
	          }
	    }

	    // Semaphore.java
	   class Semaphore {
	    	private int count = 0;
	    	
	    	public Semaphore (int initialCount) {
	    		count = initialCount;
	    	}
	    	
	    	public synchronized void P() {
	    		while (count <= 0)
	    			try {
	    				wait();
	    			} catch (InterruptedException ex) {}
	    		--count;
	    	} 
	    	
	    	public synchronized void V() {
	    		while (count >= 1)
	    			try {
	    				wait();
	    			} catch (InterruptedException ex) {}
	    		++count;
	    		notify();
	    	} 
	    } // end Semaphore	
}

the only error at the moment is this!

The method start() is undefined for the type roundabout02.carProcess

method start() is undefined for the type roundabout02.carProcess

The compiler can not find the method: start() in the class: carProcess.
Where is that method defined?

Can you post the text of the source line where you think the start() method is defined?
I don't see start() on line 22

o.k fixed by changing line 26 from "run()" to "start()" but leaves me with a new error at carProcess line 16!

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type roundabout02 is accessible. Must qualify the allocation with an enclosing instance of type roundabout02 (e.g. x.new A() where x is an instance of roundabout02).

at roundabout.roundabout02.main(roundabout02.java:17)

Unresolved compilation problem:

That looks like an error from an IDE. I don't use that IDE.
Can you compile the code with the javac compiler and get the compiler's errors?

Edited 4 Years Ago by NormR1: n/a

I was using the indigo version of Eclipse! I'll try compiling using javac in command line. Thanks for your advice, much appreciated.

carProcess is an inner class of roundabout01 (ps - please use standard Java capitalisation to make your code easier to understand). It's not declared static, so just like any other instance member of roundabout01 you need an instance of roundabout01 to qualify any reference to carProcess. Read this next:
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Unless your instances of carProcess need access to instance variables from roundabout01 then you can probably just declare carProcess as a static class.

So,, moving on...
You have screwed up your run/start solution. You are trying to create a new Thread with your carProcess class. You were right to define a run() method in the class, so change that back again. The start() method is defined in the Thread class. If you want carProcess to be a thread then declare it as extends Thread and it will inherit the necessary method(s). Then when you call start() the method inherited from Thread will start a new Thread, and call your run() method on that new Thread.

This question has already been answered. Start a new discussion instead.