Java ScheduledExecutorService的执行速度比预期的要快

时间:2014-02-12 22:54:23

标签: java concurrency fixed scheduledexecutorservice

我目前遇到的ScheduledExecutorService执行速度超过给定时间范围的问题。

scheduleAtFixedRate表示后续执行可能会延迟,但不会在给定时间后等待。

GrabPutTask只是从源中获取信息,分配捕获时间并将其发送到数据库。由于间隔小于一秒,因此数据库条目会在重复条目方面产生错误。

有没有办法在上一个任务完成后的固定时间执行任务?

我已经阅读here,任务“聚集”在一个队列中,但没有提供满足我需求的解决方案。

private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public static void main(String[] args)
{
    //
    ..
    //
    //Creating the looping thread.
    try
    {
        scheduler.scheduleAtFixedRate(new GrabPutTask(), (long) 1, (long) (seconds * 1000), TimeUnit.MILLISECONDS);
        LOGGER.info(String.format("DBUpdater connected and running. (GetAddr: %s, SetAddr: %s, Poll Rate: %.2f, Sector Number: %s, Number of PLCs: %d)",
                                  FROM_IP.split(":", 2)[0] + ":" + fromServer.getPort(), dataSource.getURL(), seconds, SECTOR, NUMBER_OF_PLCS_ON_MASTER));
    }
    catch (IllegalArgumentException ex)
    {
        LOGGER.log(Level.SEVERE, null, ex);
        printUsage();
        System.exit(1);
    }
}

示例执行时间间隔:

Event Called: 2014/02/12 14:19:07.199
Event Called: 2014/02/12 14:19:08.199
Event Called: 2014/02/12 14:19:09.199
Event Called: 2014/02/12 14:19:10.199
Event Called: 2014/02/12 14:19:11.199
Event Called: 2014/02/12 14:19:12.199
Event Called: 2014/02/12 14:19:13.199
Event Called: 2014/02/12 14:19:14.199
Event Called: 2014/02/12 14:19:15.199
Event Called: 2014/02/12 14:19:16.199
Event Called: 2014/02/12 14:19:17.199
Event Called: 2014/02/12 14:19:18.199
...
Event Called: 2014/02/12 14:20:21.415 (A load on the server it seems)
Event Called: 2014/02/12 14:20:22.215
Event Called: 2014/02/12 14:20:23.425
Event Called: 2014/02/12 14:20:24.422
Event Called: 2014/02/12 14:20:25.276
Event Called: 2014/02/12 14:20:25.997

2 个答案:

答案 0 :(得分:3)

如果执行因任何原因而延迟,

scheduleWithFixedRate会试图“赶上”。您可以使用scheduleWithFixedDelay来保持系统从时钟开始直到上一次执行之后;延迟将累积,不会赶上。

答案 1 :(得分:0)

要睡任何时间。

try {
    Thread.sleep(someTime);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}