Skip to main content

i need help

1 reply [Last post]
mike_lawson
Offline
Joined: 2006-02-17

hi im a new to java and having some problem with understanding some code, and would very much appreciate it if you could help me. my lecturer has given us the piece of code and asked us to explain it. im puttin in the piece of code in below could you please comment it and give a brief discription of how it works i.e. what are the classes the methods where mutual exclusion is used etc etc.

package traf;

import java.util.Vector;

import java.util.Random;

public class traf {

public static void main (String[] args) throws Exception{
new traf ();
}
public traf() {

City myCity = new City (50);

VehicleMaker vMaker = new VehicleMaker(myCity,50);
CheckPoint cp;
Vector q;

for(int i=0;i<10;i++) {
cp = new CheckPoint("CheckPoint "+i,myCity);
myCity.addCheckPoint(cp);
}
vMaker.start();
myCity.startCheckPoints();

}

private class City {

private int mNumVehicle;
private int mMaxNumVehicle;
private Vector mCheckPointList;
private Vector mVehicleList;

public Object mLock;

public City (int m) {
mNumVehicle = 0;
mMaxNumVehicle = m;
mCheckPointList = new Vector();
mVehicleList = new Vector();

}

public void vehicleEnters(Vehicle v) {
mNumVehicle++;
if(mNumVehicle >= mMaxNumVehicle) {
swapCheckPointSign();
}
System.out.println("Vechicle(s) in the City(Enter): "+mNumVehicle);
for(int i=mVehicleList.size()-1;i >= 0;i--) {
if(!((Vehicle)mVehicleList.elementAt(i)).isAlive()) {
mVehicleList.remove(i);
}
}
v.start();
mVehicleList.addElement(v);
}

public void vehicleLeaves() {
mNumVehicle--;
if(mNumVehicle == mMaxNumVehicle-1) {
swapCheckPointSign();
}
System.out.println("Vehicle(s) in the City(on Exit): "+mNumVehicle);
if(mNumVehicle<0) {
throw new RuntimeException("Number of Vehicles"+ mNumVehicle);
}
}

public Vector getCheckPoints() {
return mCheckPointList;
}

public void addCheckPoint(CheckPoint pCheckPoint) {
mCheckPointList.add(pCheckPoint);
}

public void swapCheckPointSign() {
for(int i=0; i < mCheckPointList.size();i++) {
((CheckPoint)mCheckPointList.elementAt(i)).swapSign();
}
}

public void startCheckPoints() {
for(int i=0; i < mCheckPointList.size();i++) {
((CheckPoint)mCheckPointList.elementAt(i)).start();
}
swapCheckPointSign();
}
}

private class CheckPoint extends Thread {
private String msName;
private boolean mbIsGreen;
private Vector mVQueue;
private City mCity;

public CheckPoint (String psName,City c) {
msName = psName;
mbIsGreen = false;
mVQueue = new Vector();
mCity = c;
}

public synchronized void vehicleLeaves(Vehicle v) {
synchronized(mCity) {
System.out.println(msName+": "+v+ " Leaves.");
mCity.vehicleLeaves();
}
}

public void addVechicleToQueue(Vehicle v) {
synchronized(mVQueue) {
mVQueue.addElement(v);
}
}
public Vehicle getNextVechicle() {
Vehicle v=null;
synchronized(mVQueue) {
if(mVQueue.size() > 0) {
v = (Vehicle)mVQueue.remove(0);
}
}
return v;
}
public void swapSign() {
mbIsGreen = !mbIsGreen;
}
public void run () {
Vehicle v=null;
while (true) {
if(v==null) {
v = getNextVechicle();
}
if( v != null ) {
synchronized(mCity) {
if(mbIsGreen) {
System.out.println(msName+": "+v + " Enters.");
mCity.vehicleEnters(v);
v=null;
}
}
}
}
}
}

private class Vehicle extends Thread {
private String msPlate;
private int needTime;
private CheckPoint mcpExit;

public Vehicle (String psPlate, int t,CheckPoint cp) {
msPlate = psPlate;
needTime = t;
mcpExit = cp;
}

public void run () {
try {
sleep(needTime);
} catch (InterruptedException e) {
System.err.println(msPlate + " - Person Interrupted");
}
mcpExit.vehicleLeaves(this);
}

public String toString() {
return msPlate;
}
}

private class VehicleMaker extends Thread {
private Random mRCheckPoint;
private Random mRTime;
private Random mRDelay;
private City city;
private int numVehicle;

public VehicleMaker(City c, int n) {
mRCheckPoint = new Random();
mRTime = new Random();
mRDelay = new Random();
numVehicle = n;
city = c;
}

public void run () {
int rq;
int rs;
int rd;
int iEntryCheckPoint;
int iExitCheckPoint;
Vector q = city.getCheckPoints();
CheckPoint cp;
for (int i=0;i

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
brinkley
Offline
Joined: 2003-06-06

This topic doesn't belong in the JavaHelp forum. Moved here.

binky