0

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?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
niz
  • 19
  • 1
  • 7

2 Answers2

2

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.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Thanks for your swift answer ... but how can I instruct the OS to wake up at the time I want to run the program? Is there any way? – niz Jul 05 '13 at 08:26
  • This is completely OS dependent, so there is no _one_ answer to that. And it depends how "deep" the computer sleeps, too. I don't believe there is any hardware/software standard defining that at all; ACPI only defines sleep _states_, not protocols on how you enter/exit sleep. – fge Jul 05 '13 at 08:28
  • So, the only way is to make sure that your computer doesn't hibernate or go to sleep while running the program... There is no automatic or programmatic way to ensure so!! – niz Jul 05 '13 at 08:32
  • Maybe there is... But it is OS dependent. You should research about this ;) – fge Jul 05 '13 at 08:36
0

ScheduledExecutorService is an interface that defines the behaviour of a task executor and ScheduleAtFixedRate() is method of this interface which expects the implementation class i.e the executor to execute the input task at fixed interval.

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

Drona
  • 6,886
  • 1
  • 29
  • 35