Scheduled executor: poll for result at fix rate and exit if timeout or result valid

前端 未结 2 1667
陌清茗
陌清茗 2020-12-19 16:11

Problem: I have a requirement to call a dao method at fix rate say every 10 sec, then I need to check if the result is valid if yes exit, else keep on call

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 17:09

    A slightly simpler method, you don't need a separate executor service for the terminator, you could simply push the terminator task into the same executor.

    Even simpler. Have PollerTask place it's result in a BlockingQueue. Then have the PollingTaskRunner do a timed poll on that BlockingQueue. Whenever control is returned from the poll call ScheduledFuture.cancel because the task either succeeded or timed out.

提交回复
热议问题