Call a method at fixed time in Java

后端 未结 3 765
-上瘾入骨i
-上瘾入骨i 2020-12-30 18:15

How do I call a method at a particular time?

For example to call the method at 6:00 and 13:00.

I\'m working at a desktop application for Windows.

3条回答
  •  醉酒成梦
    2020-12-30 18:39

    Since Java 1.5 there is a preferable way, if you need to be more strict: ScheduledThreadPoolExecutor:

    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 you choose between scheduleAtFixedRate() and scheduleWithFixedRate(). More details on the usage can be found in the linked javadoc.

提交回复
热议问题