static long roundUpDelay ( long delay , TimeUnit unit , long now ) { if ( delay < 0 ) { // Negative is treated as 0. delay = 0 ; } long target = now + unit . toMillis ( delay ) ; if ( target < now ) { // We can't add the delay to the current time without overflow. // Return the delay unaltered. return delay ; } long remainder = target % PERIOD_MILLISECONDS ; if ( remainder == 0 ) { // Already rounded. return delay ; } long extra = PERIOD_MILLISECONDS - remainder ; long newDelay = delay + unit . convert ( extra , TimeUnit . MILLISECONDS ) ; if ( newDelay < delay ) { // We can't round up without overflow. Return the delay unaltered. return delay ; } return newDelay ; }
Round up delays so that all tasks fire at approximately with approximately the same 15s period .