简体   繁体   中英

Proper way of implementing “timers” in Java?

I need to do some periodic checks in Java code. Some things get executed in separate threads, and there I need to check things every 5 seconds, then some other things every 10 minutes, etc.

How should I implement these "timers" in Java? Should I just loop and then send threads to sleep for some time?

What would be the proper way of doing it?

您应该使用ScheduledThreadPoolExecutor ,它正是这样做的。

You may need to consider using ExecutorService implementation ScheduledThreadPoolExecutorService

As per javadoc :

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required

There are a number of ways to do this, I would use a ScheduledExecutorService

ScheduledExecutorService scheduler = ...

final ScheduledFuture<?> fiveSecHandle =
   scheduler.scheduleAtFixedRate(task5, 5, 5, SECONDS);
final ScheduledFuture<?> beeperHandle =
   scheduler.scheduleAtFixedRate(task10, 10, 10, SECONDS);

 scheduler.schedule(new Runnable() {
   public void run() { beeperHandle.cancel(true); }
 }, 60 * 60, SECONDS);

Timers are generally provided by GUI frameworks since most GUI framework requires that all drawing commands and GUI modifications be issued only on the main thread. Other similar frameworks that have similar requirements (ie certain tasks to be done only under certain conditions) would also provide timer implementation that are compatible with the restrictions imposed by the framework. Use the timer provided by the framework you need to be compatible with whenever possible.

Otherwise if you want to implement it yourself, there are at least two common ways to implement timers, one is to use threads that sleeps until the next event, the other, more common in GUI framework, is to use event loop. Another common way, for very long running tasks in where interval precision is not really important, is to use cron (Unix and Unix-like systems) or Scheduled Tasks (Windows) or Alarm Intents (Android).

What would be the proper way of doing it?

Ignore what others said about specific way to do it, the proper way to do time depends on what you're trying to do. Whether you want short or long interval, whether you need very precise timing or if you're fine with slightly late and/or early event, whether you need to care about saving battery/CPU, whether you need fixed starting interval or fixed end-to-start interval, whether the task finishes quickly or if it may take some time to finish, whether you want to allow another task to start before the previous one starts, whether you want persistence (ie whether you want to be able to continue the timer after system reboot or process kill). All these will affect the best Timer to use.

如果您的应用程序非常大并且您还在使用负载平衡服务器,则可以尝试使用Quartz调度程序进行时间管理活动。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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