java - Start task using calendar object where just the second is set -
using below code, i'm attempting start thread when second part of time reaches 0 - ie when next minute starts.
public class sched { public static void main(string args[]) { calendar calstart = calendar.getinstance(timezone.gettimezone("gmt+1")); calstart.set(calendar.second, 0); scheduledexecutorservice scheduler = executors.newscheduledthreadpool(1); scheduler.scheduleatfixedrate(new sched().new rc(), calstart.gettime().gettime(), 3, timeunit.seconds); } private class rc implements runnable { public void run() { /* impl */} } }
but thread not starting. appears not setting start time correctly. run setting initialdelay
param 0
, why setting using calendar object not working?
fyi, hardcoding initialdelay
0 works (tested on jdk8):
scheduler.scheduleatfixedrate(new sched().new rc(), 0, 3, timeunit.seconds);
i don't know why you're using calendar
object (actually, don't know why anyone use one, that's story).
try using integer arithmetic on current time calculate number of seconds till next minute:
scheduler.scheduleatfixedrate(new sched().new rc(), 60 - system.currenttimemillis() / 1000 % 60, 3, timeunit.seconds);
timezone irrelevant code, since seconds part of date same no matter timezone you're in.
Comments
Post a Comment