简体   繁体   English

在一定延迟后重新执行Java程序

[英]Re-executing a Java program after a certain delay

I want to execute a Java program in Eclipse multiple times with a certain delay. 我希望在Eclipse中多次执行Java程序并且有一定的延迟。 I was trying to use ScheduleAtFixedRate() to re-execute the program after a certain interval of time. 我试图使用ScheduleAtFixedRate()在一段时间后重新执行该程序。 So what is the main difference between ScheduleAtFixedRate() and ScheduledExecutorService ? 那么ScheduleAtFixedRate()ScheduledExecutorService之间的主要区别是什么?

What is the advantage of using the latter? 使用后者有什么好处? Does it continue running the schedule of execution when the computer is set on a sleep mode? 当计算机设置为睡眠模式时,它是否继续运行执行计划?

Provided you mean .scheduleAtFixedRate() (note the little s ), then it is a method provided by ScheduledExecutorService . 如果你的意思是.scheduleAtFixedRate() (请注意小s ),那么它是ScheduledExecutorService提供的方法 As such, there is no {dis,}advantage to using either. 因此,使用任何一种都没有{dis,}优势。

You can create a ScheduledExecutorService by calling, for instance: 您可以通过调用创建ScheduledExecutorService ,例如:

final ScheduledExecutorService service
    = Executors.newScheduledThreadPool(...);
service.scheduleAtFixedRate(...);

As to: 至于:

Does it continue running the schedule of execution when the computer is set on a sleep mode? 当计算机设置为睡眠模式时,它是否继续运行执行计划?

No. It is the OS which puts the computer to sleep, and it is the OS which you should instruct to wake up at the time(s) you want. 不是。操作系统让计算机进入睡眠状态,操作系统应该指示您在所需的时间唤醒。 A running Java program is a JVM is a process is ultimately controlled by the OS. 运行Java程序是JVM的一个过程,最终由OS控制。

ScheduledExecutorService is an interface that defines the behaviour of a task executor and ScheduleAtFixedRate() is method of this interface which expects the implementation class ie the executor to execute the input task at fixed interval. ScheduledExecutorService是定义任务执行程序行为的接口,ScheduleAtFixedRate()是此接口的方法,它期望实现类即执行程序以固定间隔执行输入任务。

When your computer goes to sleep or hibernates nothing will execute. 当您的计算机进入睡眠状态或休眠状态时,将执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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