Quote:
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.