|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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)).swapSig n(); } } 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<numVehicle;i++) { iEntryCheckPoint = Math.abs(mRCheckPoint.nextInt()) % q.size(); iExitCheckPoint = Math.abs(mRCheckPoint.nextInt()) % q.size(); cp = (CheckPoint) q.elementAt(iEntryCheckPoint); cp.addVechicleToQueue(new Vehicle("Vehicle "+i,Math.abs(mRTime.nextInt()%50),(CheckPoint) q.elementAt(iExitCheckPoint))); System.out.println("Made Vehicle "+i); try { sleep(Math.abs(mRDelay.nextInt())%10); } catch (InterruptedException e) { System.err.println("People making Interrupted"); } } } } } |
|
#2
|
||||
|
||||
|
I don't know a great deal about java, but if you've got any English papers you need written, I'm up for it.
|
|
#3
|
|||
|
|||
|
all i need is the code commented and a brief discription of what is happening. the program is bassically to develop a Java application programme that makes use of
the some of the concurrency features of the Java language: threads and mutual exclusion. Task Specification A city’s local authority is looking to manage the congestion caused by too many vehicles entering the city, by limiting the total number of vehicles that can be present within the city walls at any one time. It will achieve this by counting vehicles entering and leaving the city using sensors built into the road at checkpoints around the city perimeter. Traffic lights will be used to restrict access to the city at each checkpoint, changing from green to red when the total number of vehicles within the walls exceeds some pre-determined maximum. |
|
#4
|
||||
|
||||
|
Um... are you getting an error with this code?
Or do you expect us to do you homework for you? Give a man a fish, he'll eat for a day... Teach a man to fish, he'll eat forever |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > i need help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|