简体   繁体   English

每X秒运行一次代码(Java)

[英]Run code every X seconds (Java)

This is not super necessary, I am just curious to see what others think. 这不是超级必要的,我只是想看看别人的想法。 I know it is useless, it's just for fun. 我知道这没用,只是为了好玩。

Now I know how to do this, it's fairly simple. 现在我知道该怎么做了,这很简单。 I am just trying to figure out a way to do this differently that doesn't require new variables to be created crowding up my class. 我只是想找出一种方法来以不同的方式进行操作,不需要创建新的变量来挤满我的班级。

Here's how I would do it: 这是我的处理方式:

float timePassed = 0f;
public void update(){
    timePassed += deltatime;//Deltatime is just a variable that represents the time passed from one update to another in seconds (it is a float)
    if(timePassed >= 5){
        //code to be ran every 5 seconds
        timePassed -= 5f;
    }
}

What I want to know is if there is a way to do this without the time passed variable. 我想知道的是,是否有没有时间传递变量的方法。 I have a statetime (time since loop started) variable that I use for other things that could be used for this. 我有一个statetime(自循环启动以来的时间)变量,可用于其他可用于此目的的变量。

If the goal really is to run code every X seconds, my first choice would be to use a util.Timer. 如果目标确实是每X秒运行一次代码,那么我的首选是使用util.Timer。 Another option is to use a ScheduledExecutorService which adds a couple enhancements over the util.Timer (better Exception handling, for one). 另一种选择是使用ScheduledExecutorService,它在util.Timer上增加了一些增强(一个更好的异常处理)。

I tend to avoid the Swing.Timer, as I prefer to leave the EDT (event dispatch thread) uncluttered. 我倾向于避免使用Swing.Timer,因为我希望使EDT(事件分配线程)保持整洁。

Many people write a "game loop" which is closer to what you have started. 许多人会写一个“游戏循环”,它与您开始时的游戏更加接近。 A search on "game loop" will probably get you several variants, depending on whether you wish to keep a steady rate or not. 搜索“游戏循环”可能会为您提供多种变体,具体取决于您是否希望保持稳定的速度。

Sometimes, in situations where one doesn't want to continually test and reset, one can combine the two functions via the use of an "AND" operation. 有时,在不想持续测试和重置的情况下,可以通过使用“ AND”操作将这两个功能结合在一起。 For example, if you AND 63 to an integer, you have the range 0-63 to iterate through. 例如,如果您将63与整数进行AND运算,则您可以在0-63的范围内进行迭代。 This works well on ranges that are a power of 2. 这在2的幂的范围上效果很好。

Depending on the structure of your calling code, you might pass in the "statetime" variable as a parameter and test if it is larger than your desired X. If you did this, I assume that a step in the called code will reset "statetime" to zero. 根据调用代码的结构,您可以传递“ statetime”变量作为参数,并测试它是否大于所需的X。如果这样做,我假设被调用代码中的某个步骤将重置“ statetime” ”归零。

Another idea is to pass in a "startTime" to the update method. 另一个想法是将“ startTime”传递给更新方法。 Then, your timer will test the difference between currentTimeMillis and startTime to see if X seconds has elapsed or not. 然后,您的计时器将测试currentTimeMillis和startTime之间的差异,以查看是否经过了X秒。 Again, the code you call should probably set a new "startTime" as part of the process. 同样,您调用的代码可能应该在过程中设置一个新的“ startTime”。 The nice thing about this method is that there is no need to increment elapsed time. 这种方法的好处是无需增加经过时间。

As long as I am churning out ideas: could also create a future "targetTime" variable and test if currentTimeMillis() - targetTime > 0. 只要我提出想法:还可以创建将来的“ targetTime”变量,并测试currentTimeMillis()-targetTime> 0。

startTime or targetTime can be immutable, which often provides a slight plus, depending on how they are used. startTime或targetTime可以是不可变的,这通常会略有加号,具体取决于使用方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM