Would anyone be able to help me with a little problem I'm experiencing while producing a concurrent scenario in Java (using monitors).
Basically I have a variable, numPumps (in PumpMon.java) which is decremented each time a car enters the petrol station. I have attempted to make this variable global and call it in car.java as in:
System.out.println("There are" + numPumps + "available at the station");
Although this presents the error that a non-static variable cannot be used in a static expression. This makes sense given that numPumps value can change so many time during the program execution.
How would I go about assigning the current value of numPumps to a static variable so I can display the value of numPumps at different times throughout execution?
Cheers for any pointers.
This is the runnable program that shows you the scenario being demonstrated.
Basically I have a variable, numPumps (in PumpMon.java) which is decremented each time a car enters the petrol station. I have attempted to make this variable global and call it in car.java as in:
System.out.println("There are" + numPumps + "available at the station");
Although this presents the error that a non-static variable cannot be used in a static expression. This makes sense given that numPumps value can change so many time during the program execution.
How would I go about assigning the current value of numPumps to a static variable so I can display the value of numPumps at different times throughout execution?
Cheers for any pointers.
Code:
public class PumpMon {
private int numPumps = 2;
PumpMon(int num) {
numPumps = num;
}
synchronized void enter() {
while (numPumps <=0) {
try { wait(); }
catch (InterruptedException e) {}
}
numPumps--;
}
synchronized void exit() {
numPumps++;
notify();
}
}
Code:
public class Car extends Thread {
private String name;
private PumpMon mon;
public Car(String name, PumpMon mmm) {
this.name = name;
mon = mmm;
}
public void run() {
System.out.println("There are available at the station");
System.out.println(name + " about to try entering the forecourt");
mon.enter();
System.out.println(name + " is at the petrol pump");
PetrolStationActivity.idleQuietly(8000);
System.out.println(name + " driver has paid, and has left the petrol pump");
mon.exit();
System.out.println(name + " has left the forecourt");
}
}
Code:
public class PetrolStationActivity {
public static void idle(int millisecs) {
Thread thisThread = Thread.currentThread();
System.out.println(thisThread.getName() + ": About to sleep");
try {
thisThread.sleep(millisecs);
}
catch (InterruptedException e) { }
System.out.println(thisThread.getName() + ": Woken");
}
public static void idleQuietly(int millisecs) {
Thread thisThread = Thread.currentThread();
try {
thisThread.sleep(millisecs);
}
catch (InterruptedException e) { }
}
}
This is the runnable program that shows you the scenario being demonstrated.
Code:
public class PetrolStationSolution {
public static void main(String[] args) {
PumpMon mon = new PumpMon(4);
Car c1 = new Car("Volvo Reg. Number IKZ4463", mon);
Car c2 = new Car("Peugeot Reg. Number IAZ4563", mon);
Car c3 = new Car("BMW Reg. Number AAZ6474", mon);
Car c4 = new Car("Audi Reg. Number IIA43", mon);
Car c5 = new Car("Ford Reg. Number IKZ5453", mon);
Car c6 = new Car("Porsche Reg. Number AAB1", mon);
Car c7 = new Car("Jaguar Reg. Number BIG4423", mon);
c1.start();
c2.start();
c3.start();
c4.start();
c5.start();
c6.start();
c7.start();
}
}