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

com.kolibrifx.plovercrest.server.internal.protocol.TimeoutProvider Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
 */

package com.kolibrifx.plovercrest.server.internal.protocol;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import com.kolibrifx.common.Disposable;
import com.kolibrifx.common.dispatcher.ScheduledExecutorBuilder;

public class TimeoutProvider implements Disposable {
    private final ScheduledExecutorService scheduledExecutorService =
            new ScheduledExecutorBuilder("timeout-provider").build();

    /**
     * Returns an {@link AtomicBoolean} timeout flag, initially set to false. It will
     * be automatically set to true after the given delay.
     */
    public AtomicBoolean createTimeoutFlag(final long delay, final TimeUnit unit) {
        final AtomicBoolean timedOut = new AtomicBoolean(false);
        scheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                timedOut.set(true);
            }
        }, delay, unit);
        return timedOut;
    }

    @Override
    public void close() {
        scheduledExecutorService.shutdownNow();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy