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

org.reactfx.ScheduledExecutorServiceTimer Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.reactfx;

import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.reactfx.util.Timer;
import org.reactfx.util.TriFunction;

class ScheduledExecutorServiceTimer implements Timer {

    public static Timer create(
            java.time.Duration timeout,
            Runnable action,
            ScheduledExecutorService scheduler,
            Executor eventThreadExecutor) {
        return new ScheduledExecutorServiceTimer(
                timeout, action,
                (delay, unit, cmd) -> scheduler.schedule(cmd, delay, unit),
                eventThreadExecutor);
    }

    public static Timer createPeriodic(
            java.time.Duration timeout,
            Runnable action,
            ScheduledExecutorService scheduler,
            Executor eventThreadExecutor) {
        return new ScheduledExecutorServiceTimer(
                timeout, action,
                (delay, unit, cmd) -> scheduler.scheduleAtFixedRate(cmd, delay, delay, unit),
                eventThreadExecutor);
    }

    private final long timeout;
    private final TimeUnit unit;
    private final Runnable action;
    private final TriFunction> scheduler;
    private final Executor eventThreadExecutor;

    private ScheduledFuture pendingTimer = null;
    private long seq = 0;

    private ScheduledExecutorServiceTimer(
            java.time.Duration timeout,
            Runnable action,
            TriFunction> scheduler,
            Executor eventThreadExecutor) {

        this.timeout = timeout.toNanos();
        this.unit = TimeUnit.NANOSECONDS;
        this.action = action;
        this.scheduler = scheduler;
        this.eventThreadExecutor = eventThreadExecutor;
    }

    @Override
    public final void restart() {
        stop();
        long expected = seq;
        pendingTimer = scheduler.apply(timeout, unit, () -> {
            eventThreadExecutor.execute(() -> {
                if(seq == expected) {
                    action.run();
                }
            });
        });
    }

    @Override
    public final void stop() {
        if(pendingTimer != null) {
            pendingTimer.cancel(false);
        }
        ++seq;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy