com.kolibrifx.plovercrest.server.internal.protocol.TimeoutProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plovercrest-server Show documentation
Show all versions of plovercrest-server Show documentation
Plovercrest server library.
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();
}
}