简体   繁体   中英

Re-executing a Java program after a certain delay

I want to execute a Java program in Eclipse multiple times with a certain delay. I was trying to use ScheduleAtFixedRate() to re-execute the program after a certain interval of time. So what is the main difference between ScheduleAtFixedRate() and 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 . As such, there is no {dis,}advantage to using either.

You can create a ScheduledExecutorService by calling, for instance:

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.

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.

When your computer goes to sleep or hibernates nothing will execute.

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