在Java中等待
我有一个模拟汽车结构的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我对你的问题的假设:
你想以 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.
这有点复杂,但应该可以很好地扩展。
您可以通过创建更多事件(即启动或拆卸事件)来扩展
This is somewhat more complicated, but should scale well.
You can extend by making more events (i.e. a start up or tear down event)