All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kyo.scheduler.InternalTimer.scala Maven / Gradle / Ivy

package kyo.scheduler

import InternalTimer.*
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration

abstract private[kyo] class InternalTimer {
    def schedule(interval: Duration)(f: => Unit): TimerTask
    def scheduleOnce(delay: Duration)(f: => Unit): TimerTask
}

private[kyo] object InternalTimer {

    abstract class TimerTask {
        def cancel(): Boolean
    }

    def apply(executor: ScheduledExecutorService): InternalTimer =
        new InternalTimer {
            def schedule(interval: Duration)(f: => Unit): TimerTask = {
                val future = executor.scheduleWithFixedDelay(() => f, interval.toNanos, interval.toNanos, TimeUnit.NANOSECONDS)
                () => future.cancel(true)
            }

            def scheduleOnce(delay: Duration)(f: => Unit): TimerTask = {
                val future = executor.schedule((() => f): Runnable, delay.toNanos, TimeUnit.NANOSECONDS)
                () => future.cancel(true)
            }
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy