Commit 172ae79c authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Parking Cash Example

parent 49282f9a
package ParkingCash;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) throws InterruptedException {
ParkingCash cash = new ParkingCash();
ParkingStats stats = new ParkingStats(cash);
System.out.printf("Parking Simulator\n");
int numberSensors = 2 * Runtime.getRuntime().availableProcessors();
System.out.println("Num of Sensors " + numberSensors);
ExecutorService executorService = Executors.newFixedThreadPool(numberSensors);
long start = System.currentTimeMillis();
for (int i = 0; i < numberSensors; i++) {
executorService.submit(new Sensor(stats));
}
executorService.shutdown();
executorService.awaitTermination(3, TimeUnit.SECONDS);
long end = System.currentTimeMillis() - start;
System.out.printf("Number of cars: %d\n",
stats.getNumberCars());
System.out.printf("Number of motorcycles: %d\n",
stats.getNumberMotorcycles());
cash.close();
System.out.println();
System.out.println("Time Execution: "+ end);
}
}
package ParkingCash;
public class ParkingCash {
private static final int cost = 2;
private long cash;
public ParkingCash() {
cash = 0;
}
public synchronized void vehiclePay() {
cash += cost;
}
public void close() {
System.out.printf("Closing accounting");
long totalAmount;
synchronized (this) {
totalAmount = cash;
cash = 0;
}
System.out.printf("The total amount is : %d",
totalAmount);
}
}
package ParkingCash;
public class ParkingStats {
private final Object controlCars, controlMotorcycles;
private long numberCars;
private long numberMotorcycles;
private ParkingCash cash;
public ParkingStats(ParkingCash cash) {
numberCars = 0;
numberMotorcycles = 0;
this.cash = cash;
this.controlCars = new Object();
this.controlMotorcycles = new Object();
}
public void carComeIn() {
synchronized (controlCars) {
numberCars++;
}
}
public void carGoOut() {
synchronized (controlCars) {
numberCars--;
}
cash.vehiclePay();
}
public void motoComeIn() {
synchronized (controlMotorcycles) {
numberMotorcycles++;
}
}
public void motoGoOut() {
synchronized (controlMotorcycles) {
numberMotorcycles--;
}
cash.vehiclePay();
}
public long getNumberCars() {
synchronized (controlCars) {
return numberCars;
}
}
public long getNumberMotorcycles() {
synchronized (controlMotorcycles) {
return numberMotorcycles;
}
}
}
package ParkingCash;
import java.util.concurrent.TimeUnit;
public class Sensor implements Runnable {
private ParkingStats stats;
public Sensor(ParkingStats stats) {
this.stats = stats;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
stats.carComeIn();
stats.carComeIn();
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
stats.motoComeIn();
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
stats.motoGoOut();
stats.carGoOut();
stats.carGoOut();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment