Implement Gas Station in Java?

Follow
  • Follow publicly
  • Follow privately
  • Unfollow
I have to write a program to implement a gas station in java. The gas station has 5 pumps and each pump has a queue that the cars wait in if the pump is occupied. The cars arrive at ...show more
Best Answer
This sounds like a question about data structures. And a fun project :)
I think the most straightforward way would to have a single thread "generating" the cars. For each pump, you can store a java.util.Queue<Car> to keep track of them. You can either have a seperate thread for each pump or have a scheduler thread of some sort (maybe the same one as generating the cars). This way you can "enqueue" the cars from the producer thread and "dequeue" them in the consumer thread(s).

Sample for creation:

Thread creator = new Thread(new Runnable(){

public void run(){
while(programRunning){
Car car = createRandomCar();
Pump pump = choosePump();
car.goto(pump); //car implementation enqueues itself here in the pump
Thread.currentThread().sleep(Math.rand... 20000));
}
}

});
creator.start();

//continue in program
  • Rate
  • Comment
Sign In 

to add your answer

No other answers

Who is following this question?

    %
    BEST ANSWERS
    Member Since:
    Points: Points: Level
    Total Answers:
    Points this week:
    Follow
     
    Unfollow
     
    Block
     
    Unblock