简体   繁体   English

安排24个不同计时器的最有效方法

[英]Most efficient way to schedule 24 different timers

I currently am using Bukkit for plugin development, and I have 12 preferences that are configured in a file (12 ways a person can pay to fly.) 我目前正在使用Bukkit进行插件开发,并且我在文件中配置了12个首选项(一个人可以付费使用的12种方式)。

For each configuration (12), there are 2 timers needed. 对于每种配置(12),需要2个计时器。 One 'withdraw' timer, and one 'fly check' timer. 一个“退出”计时器和一个“飞行检查”计时器。 Each timer would be setup at different intervals, so I couldn't combine the two. 每个计时器的设置时间间隔都不同,所以我无法将两者结合在一起。

Then, when a player has a permission for one of the 12 configurations, he/she is placed in an Array, and the timer assigned to that configuration iterates through the array, running the methods for checking what's needed. 然后,当玩家获得12种配置之一的许可时,他/她被放置在一个数组中,分配给该配置的计时器在数组中迭代,运行用于检查所需内容的方法。

However, I believe there's a better way to do this, besides having 24 timers go off on server start. 但是,我相信,除了在服务器启动时关闭24个计时器外,还有更好的方法。 It just seems like it's too much, and will be a major hit to performance. 看起来好像太多了,它将对性能产生重大影响。 The timers must also be SyncDelayedTask because they access the Bukkit API. 计时器也必须是SyncDelayedTask,因为它们访问Bukkit API。

So I was wondering if there's a more efficient way to solve this problem? 所以我想知道是否有更有效的方法来解决这个问题?

There is a way that you could do this without setting 24 separate timers. 您可以通过一种方法来设置24个独立的计时器。 You could create one, with the time interval being 1 . 您可以创建一个,时间间隔为1 Then, you could add one to a number every time the timer runs. 然后,您可以在每次计时器运行时向数字加1。 Like this: 像这样:

int loops;

this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){

          public void run() {
              loops++;//add one to the loops
          }
        },1, 1);

Then, for every time interval, you could set up a check to see if loops is divisible by the interval... For example, let's say that you would like to have a fly check every 5 ticks, and your withdraw timer every 7 ticks. 然后,对于每个时间间隔,您可以设置一个检查以查看loops是否可被该间隔整除...例如,假设您希望每5个滴答声有一次fly check ,而每7个滴答声要有一次withdraw计时器。 You could do this: 您可以这样做:

int loops;

this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){

          public void run() {
              loops++;//add one to the loops

                      if(loops %  5 == 0){//if loops is divisible by 5
                          //fly check code
                      }
                      if(loops % 7 == 0){//if loops is divisible by 7
                          //withdraw code
                      }
          }
        },1, 1); //make the timer repeat every 1 tick

Just make sure that you only use if statements, not else if . 只要确保只使用if语句,不要使用else if If you use else if above, and loops is 35, which is divisible by 5 and 7, it will run the first statement, but not the second, because you are telling it to run that statement only if the above ones are false. 如果else if上面使用else if ,并且循环是35(可被5和7整除),它将运行第一个语句,但不会运行第二条语句,因为您告诉它仅在以上条件为false时才运行该语句。 Therefor, if you only use if statements, then it will be able to check every single one of them, and multiple will be able to be called. 因此,如果仅使用if语句,则它将能够检查其中的每个语句,并且可以调用多个语句。

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

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