ImranRazaKhan
ImranRazaKhan

Reputation: 2297

How to restrict ScheduledExecutorService

I have following code which call a task after every 20 min and its working fine. Now above this i want that it only work between 0900 to 1800

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleWithFixedDelay(new CSDelightAlertTask(), 0, 20, TimeUnit.MINUTES);

Upvotes: 0

Views: 116

Answers (2)

assylias
assylias

Reputation: 328608

If you can use an external library, cron4j can help for that kind of complex scheduling. Probably not worth it if this is a one off.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198093

The simplest solution would just be to wrap CSDelightAlertTask in a wrapper that only runs the underlying task if and only if the time is between 0900 and 1800.

Upvotes: 4

Related Questions