scheduleAtFixedRate未在指定的延迟时间启动任务 [英] scheduleAtFixedRate not starting the task at specified delay

查看:629
本文介绍了scheduleAtFixedRate未在指定的延迟时间启动任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SchedulerExecuterService在指定的延迟后并以给定的时间间隔执行任务.

I am using SchedulerExecuterService to execute a task after specified delay and at given intervals.

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(taskThread,60 ,120 ,TimeUnit.SECONDS);

发生的是第一次taskThread在60秒的延迟后没有启动,而是在超过60秒的延迟后才启动.而下一次执行将以120秒的正确间隔开始(不是精确的120秒,但是会有非常微小的延迟,可以忽略).

What happening is first time taskThread is not starting after delay of 60 sec, it is started after delay of more than 60 secs. Whereas the next executions started at correct intervals of 120 secs(not exactly 120 secs but there is a very minute delay which can be ignored).

我的查询是为什么第一次执行会延迟60秒以上?由于任务是由线程执行的,因此启动时间是否取决于线程的优先级?

My query is why the first execution is delayed for more than 60 secs? Since the task is executed by a thread, Does the start up time depends on the thread priority?

如何使它以60秒的确切延迟运行?石英库呢?该库是否可以解决我的目的(在特定时间运行该作业而没有任何延迟)?

How can I make it run at exact delay of 60 secs? What about Quartz library? Will this library solve my purpose(run the job at specific time without any delay)?

先谢谢了.

推荐答案

T.b.h.您看到的延迟不太可能是ScheduledExecutorService的错误.除非您的计算机过载,否则启动线程将不会花费一秒钟的时间,而且我认为,如果您使用固定大小的线程池,则无论如何该线程都将被预先初始化.

T.b.h. the delay you are seeing is unlikely to be ScheduledExecutorService's fault. Starting a thread won't take a second unless your machine is ridiculously overloaded, and I think the thread will get initialised up-front anyways if you use a fixed size thread pool.

我猜这是您自己的任务的初始化.我不知道您在执行任务时要做什么,但是准备好资源(启动数据库连接池)等可能会花费大量时间.

I'm guessing it's the initialisation of your own tasks. I don't know what you are doing in your task, but getting the resource ready (starting DB connection pools) etc. can amount to significant time.

如果需要,您可以使用一个琐碎的任务(例如在控制台上编写"hello")进行测试以进行查看.然后,您可以衡量任务的每个部分,以了解需要花费多长时间.了解这些信息后,您就可以想到加热"系统以防止延迟的方法.

If you want you can test with a trivial task (like writing "hello" on the console) to see. And then you can measure each part of your task to see what's taking long. Once you have that you can think of ways to "warm" your system to prevent the delay.

这篇关于scheduleAtFixedRate未在指定的延迟时间启动任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆