Results 1 to 2 of 2

Thread: CarPark Moniter

  1. #1
    newfish is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default CarPark Moniter

    Hi all i have an assignment due in tomorrow and im not sleeping until this is done. I have been extremely lazy so cutting the bs i need some help if i can get it.
    I have a carpark control problem(assignment) that needs to model a car park having 3000 spaces and 3 entrances and 3 exits. 3 threads must be created to simulate the arrival and a futher 3 threads for departure. each thread sepps for a random (0-1s) here is what i got so far:
    it starts from CarPark class at the bottom
    class Semaphore {
    private int currentValue;
    Semaphore(int v) {
    currentValue = v;
    }
    synchronized public void down() throws InterruptedException {
    while(currentValue == 0) wait();
    currentValue--;

    }
    synchronized public void up() throws InterruptedException{
    currentValue++;
    notifyAll();

    }


    };
    abstract class CarParkMonitor {
    protected int capacity;
    protected int totalCars;
    protected long startTime;

    long elapsedTime() {
    return (System.currentTimeMillis() - startTime )/1000;
    }

    void printCars() {
    System.out.println("Cars in carpark = "+ totalCars);
    }

    abstract void arrive();
    abstract void depart();

    };

    class CarParkJavaMonitor extends CarParkMonitor {
    int x;
    public CarParkJavaMonitor(int x){
    this.x = x;
    startTime = 0;
    }
    public void arrive(){
    System.out.println("Used me");
    }

    public void depart(){

    }
    };

    class CarParkSemaphoreMonitor extends CarParkMonitor {

    public CarParkSemaphoreMonitor(int x){

    }
    public void arrive(){

    }

    public void depart(){

    }
    };

    class Arrivals extends Thread {
    private CarParkMonitor control;
    private long arrivals;
    public Arrivals(CarParkMonitor x) {
    control = x;
    arrivals = 0;
    }
    public void print() {
    System.out.println("Arrivals " + arrivals);
    }

    public void run() {
    try {

    while (control.elapsedTime() < 180) {

    control.arrive();
    arrivals++;
    sleep((int)(Math.random()*1000));
    }
    } catch (Exception e) {
    System.err.println(e);
    e.printStackTrace();
    }
    print();
    }
    };

    class Departures extends Thread {
    private CarParkMonitor control;
    private long departs;
    public void print() {
    System.out.println("Departures " + departs);
    }

    public Departures(CarParkMonitor x) {
    control = x;
    departs = 0;
    }

    public void run() {
    try {
    while (control.elapsedTime() < 180) {
    control.depart();
    departs++;
    sleep((int)(Math.random()*1000));
    }
    } catch (Exception e) {
    System.err.println(e);
    e.printStackTrace();
    }
    print();
    }
    };


    public class CarPark {
    public static void main(String args[]) {
    boolean monitor = false;

    if(args.length >=1) {
    if(args[0].compareTo(new String("-s")) == 0)
    monitor = false;
    else monitor = true;
    }else monitor = true;

    Arrivals one, two,three;
    Departures four, five,six;
    CarParkMonitor xx = null;

    if(monitor == true) xx = new CarParkJavaMonitor(1000);
    else xx = new CarParkSemaphoreMonitor(1000);
    //System.out.println("REMEMBER: your code will be marked on the basis of whether it compiles, and if it is theoretically correct, not just that it seems to give correct answers!!!!");

    one = new Arrivals(xx);
    Thread c1 = new Thread(one);
    c1.start();
    two = new Arrivals(xx);
    Thread c2 = new Thread(two);
    c2.start();
    three = new Arrivals(xx);
    Thread c3 = new Thread(three);
    c3.start();
    four = new Departures(xx);
    Thread c4 = new Thread(four);
    c4.start();
    five = new Departures(xx);
    Thread c5 = new Thread(five);
    c5.start();
    six = new Departures(xx);
    Thread c6 = new Thread(six);
    c6.start();


    while(Thread.activeCount() > 1) {
    if (xx.elapsedTime () > 200) {
    System.err.println("Time limit exceeded: interrupting threads");


    one.interrupt();
    two.interrupt();
    three.interrupt();
    four.interrupt();
    five.interrupt();
    six.interrupt();
    one.print();
    two.print();
    three.print();
    four.print();
    five.print();
    six.print();
    break;
    }
    }
    xx.printCars();

    }

    };
    im staying up all night trying to get it done if anyone can help out then that would be great otherwise i apologise for wasting ur time thank you.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    i just looked at the code it little closer and i noticed that the control.elapsedTime() inside the class Arrivals and Departures is never < 180 so the methods control.arrive() and control.depart() will never be called. and the variable startTime is never set. so when you start this code inside run() nothing happens. can you please correct this and explain what is the purpose of the elapsedTime?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •