在Java中等待

发布于 2024-12-23 09:49:51 字数 768 浏览 3 评论 0原文

我有一个模拟汽车结构的java程序。我写了一个解释,但它很难理解并且没有什么用处,所以我只问“抽象”问题。

我有一个无限循环,它调用“now”方法,告诉结构构建一辆新车。

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

织物内部有 3 个区域;第 0 阶段是创建汽车车身的阶段,第 1 阶段是为汽车提供灯光的阶段,第 2 阶段是我在其上放置保险杠的阶段,最后阶段是放置所有成品汽车的地方。

当 menthod now 第一次被调用时,它会在阶段 0 中创建汽车的车身。然后,当第二次调用它时,它将第一个车身移动到阶段 1,它放置了汽车的灯光(通过画两个圆圈)并创建了一个新的车身。以这种方式进行,直到汽车准备好,即移动到完成区域。

这是我的问题。我第二次调用 now 方法,它做了 3 件事

:它在第一阶段绘制车身
二.它在0阶段绘制了新的车身
三.它在第一阶段的车身上绘制了灯光。

我想推迟步骤 (iii),以便人们可以看到汽车配备了灯光。如果我使用 wait、thread.sleep 或执行一个需要 x 毫秒的循环,则会从那时起延迟整个结构。 我认为我需要的是从正常流程中取出 (iii) 步骤并单独进行,因此当我在绘制灯光之前等待 350 毫秒时,它不会延迟其余的制造过程。 (画的其余部分)。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗藏城府 2024-12-30 09:49:51

这是我对你的问题的假设:
你想以 500 毫秒的固定间隔调用 now() 方法。现在不管你想延迟 now() 中的子任务 3。这也意味着子任务 3 中的延迟不应延迟每 500 毫秒调用 now()

如果这是您的要求,那么我建议您使用 java.util.Timer.scheduleAtFixedRate

只需创建一个 TimerTask 并将 now() 调用包装在其中。

在子任务 3 中使用 wait(500) 或您想要的时间。

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.

心如狂蝶 2024-12-30 09:49:51

这有点复杂,但应该可以很好地扩展。

  1. 实现事件队列
  2. 让每个事件执行适当的函数调用,然后将下一个事件添加到队列
  3. 您可以通过创建更多事件(即启动或拆卸事件)来扩展

    接口 QueueEvent{
      无效执行(EventQueue q);
    }
    
    类 Stage0CarEvent 实现 QueueEvent{
      公共无效执行(EventQueue q){
        布料.drawStage0Car();
        q.push(new Stage1CarEvent(),500);
      }
    }
    
    类 Stage1CarEvent 实现 QueueEvent{
      公共无效执行(EventQueue q){
        布料.drawStage1Car();
        q.push(new AddLightsEvent(),350);
        q.push(new Stage2CarEvent(),500);
      }
    }
    
    // 其他事件...
    
    类事件队列{
      无效运行(){
        而(真){
          // 获取下一个事件
          // 等待下一个事件所需的时间
          事件.执行(此);
        }
      }
    
      无效推送(QueueEvent事件,int t){
        //从现在起插入下一个事件t时间单位
      }
    }
    

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
      }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文