skedule icon indicating copy to clipboard operation
skedule copied to clipboard

Recurring execution of Schedule's

Open LarsArtmann opened this issue 4 years ago • 1 comments

I think a method like this would be quite helpful:

fun Schedule.schedule(executor: ThreadPoolExecutor, code: () -> Unit) {
    val now = ZonedDateTime.now()
    val next = this.next(
        now
            .minusSeconds(now.second.toLong())
            .minusNanos(now.nano.toLong())
    )
    val delay = next.toEpochSecond() - now.toEpochSecond()
    executor.schedule(
        {
            this.schedule(code)
            code()
        },
        delay,
        TimeUnit.SECONDS
    )
}

because the example in the README.md is not recurring

LarsArtmann avatar Nov 25 '20 11:11 LarsArtmann

I don't like the idea of mixing in ThreadPoolExecutor (which is a Java class), you could use a Kotlin coroutine instead

Zomis avatar Jan 30 '21 14:01 Zomis