Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java concurrently generate infinite amount of objects

So I need to make an elevator simulator, and I was wondering how can I go about continuously generating people to call the elevator. I need this to go on forever. So basically a person is created and calls the elevator. All of these calls are kept track of but I think I need to keep track of the people who are actually on the elevator too.

I have a few classes Person, Elevator, ElevatorCall & ElevatorCallQueue.

  1. In Person I have a run() method which basically makes an Elevator call with the current floor and destination floor and then I have a BlockingQueue that I put the call on. This run method just runs while true.

  2. In ElevatorCall I just have getters and setters for the collection and destination floors

  3. In ElevatorCallQueue, I have variables for MAX_CALLS and numberOfPeople. I have a BlockingQueue<ElevatorCall> queue = new ArrayBlockingQueue<ElevatorCall>(MAX_CALLS) and a List<Person> I add people to the list and I run through the list and start the run() method on each person. Finally I create an elevator and provide the queue, and run it.

  4. In Elevator I have the BlockingQueue<ElevatorCalls>. I have a while(true) here also, and inside it I make an ArrayList<ElevatorCall> and then I use the BlockingQueues drainTo method using the ArrayList<ElevatorCalls> as a parameter. The rest of the run() method basically iterates through the array list and does what an elevator does, so It goes to the first pressed button, checks each floor for people and if it is a destination floor.

Right now I've gotton stuck and dont know where to go from here. I need to some how have People continiously added and calling the elevator, and have the Elevator wait if there is no more calls. Would appreciate it if anybody could help put me in the right direction. Thanks

EDIT

Here is the code to the elevator class as somebody said I should post some code. However I'm not sure what code to post so I thought I'd just put in the entire class

like image 629
Ayohaych Avatar asked Dec 24 '13 12:12

Ayohaych


1 Answers

I think everyone's jumped at the word concurrency very quickly - don't let it cloud your judgement. I can't speak on behalf your exact problem/criteria but an elevator goes about travelling to floors, with the only disturbance being a new person pressing a button. So, why not give elevator a method simulate(int time) that does this, and a method new_person(person p) which adds another person to the queue. Then just generate a random time interval, simulate the elevator, add a new person with random floor destination and source, and then repeat.

But you say it has to be concurrent -

Well your question seems to be where do the elevatorcalls come from?

This is an instance of the typical producer consumer pattern. What's that you ask? Well the oracle documentation for BlockingQueue gives a better example than I ever could

class Producer implements Runnable {
   private final BlockingQueue queue;
   Producer(BlockingQueue q) { queue = q; }
   public void run() {
     try {
       while (true) { queue.put(produce()); }
     } catch (InterruptedException ex) { ... handle ...}
   }
   Object produce() { ... }
 }

 class Consumer implements Runnable {
   private final BlockingQueue queue;
   Consumer(BlockingQueue q) { queue = q; }
   public void run() {
     try {
       while (true) { consume(queue.take()); }
     } catch (InterruptedException ex) { ... handle ...}
   }
   void consume(Object x) { ... }
 }

 class Setup {
   void main() {
     BlockingQueue q = new SomeQueueImplementation();
     Producer p = new Producer(q);
     Consumer c1 = new Consumer(q);
     Consumer c2 = new Consumer(q);
     new Thread(p).start();
     new Thread(c1).start();
     new Thread(c2).start();
   }
 }

Hmm.. Can you see how this relates to your problem? You've already solved half of it.

The Elevator acts as the consumer of elevatorCalls, you seem to be struggling with who produces them. This is a job for a new thread that runs an ElevatorCall producers. See if you can work the rest out.

like image 104
user3125280 Avatar answered Nov 15 '22 15:11

user3125280