Daniel
Daniel

Reputation: 1341

waiting in Java

I have a java program which emulates a car fabric. I wrote an explanation, but it would be pretty hard to understand and not really useful, so I'll just ask the "abstract" question.

I have an infinite loop which calls the method "now" wich tells the fabric to build a new car.

    while(true){
        try{
            Thread.sleep(500);
        }catch (Exception e) {;}
        fabric.now();
    }

Inside the fabric there are 3 zones; stage 0 where the body of the car is created, stage 1 where the car is provided with lights, stage 2 where I put a bumper on it and finished which is the place where all finished cars are placed.

When the menthod now is called for the very first time, it creates the body of the car in the stage 0. Then, when it's called for the 2nd time, it moves the first body to stage 1, it puts the lights of the car (by drawing two circles) and it creates a new body. It goes on this way until the car is ready, which is when it's moved to the finished zone.

Here is my problem. The 2nd time I call the now method, it does 3 things:

i. it paints a car-body in the 1 stage
ii. it paints a new car-body in the 0 stage
iii. it paints the lights over the car-body on the 1 stage.

I want to delay the step (iii) so people can see the car being provided with lights. If I use wait, thread.sleep, or do a cycle wich takes x milliseconds, it delays the whole fabric from then on. I think what I need is to take the (iii) step out of the normal flow and do it separately, so when I wait 350 milliseconds before painting the lights it doesn't delay the rest of the fabrication-process. (the rest of the painting).

Any ideas?

Upvotes: 1

Views: 572

Answers (2)

Pragalathan  M
Pragalathan M

Reputation: 1781

Here is my assumption about your question: you want to call now() method at regular interval of say 500ms. Now irrespective of the you want to delay the subtask 3 inside the now(). This also means the delay in subtask 3 should not delay the call to now() at every 500ms

If this is your requirement then I suggest you using java.util.Timer.scheduleAtFixedRate

Simply create a TimerTask and wrap your now() call inside it.

Use wait(500) or how much ever you want in subtask 3.

Upvotes: 3

Ryan
Ryan

Reputation: 36

This is somewhat more complicated, but should scale well.

  1. Implement an event queue
  2. Have each event perform the proper function calls, and then add the next events to the queue
  3. You can extend by making more events (i.e. a start up or tear down event)

    interface QueueEvent{
      void perform(EventQueue q);
    }
    
    class Stage0CarEvent implements QueueEvent{
      public void perform(EventQueue q){
        fabric.drawStage0Car();
        q.push(new Stage1CarEvent(),500);
      }
    }
    
    class Stage1CarEvent implements QueueEvent{
      public void perform(EventQueue q){
        fabric.drawStage1Car();
        q.push(new AddLightsEvent(),350);
        q.push(new Stage2CarEvent(),500);
      }
    }
    
    // other events...
    
    class EventQueue{
      void run(){
        while(true){
          // get the next event
          // wait an amount of time needed for the next event
          event.perform(this);
        }
      }
    
      void push(QueueEvent event,int t){
        //insert the next event t time units from now
      }
    }
    

Upvotes: 0

Related Questions