@Override protected void doStop() { this.stopToken.requestCancellation(); super.doStop(); }
@Override public void close() { ExecutorServiceHelpers.shutdown(executor, maintenanceExecutor); } }
private boolean canRun() { synchronized (this.readBuffer) { return !this.cancellationToken.isCancellationRequested() && !this.isClosed; } }
public static CompletableFuture<Void> loopWithDelay(Supplier<Boolean> condition, Supplier<CompletableFuture<Void>> loopBody, long delay, ScheduledExecutorService executor) { return Futures.loop(condition, () -> Futures.delayedFuture(loopBody, delay, executor), executor); }
/** * Waits for the provided future to be complete, and returns true if it was successful, false otherwise. * * @param f The future to wait for. * @param <T> The Type of the future's result. * @return True if the provided CompletableFuture is complete and successful. */ public static <T> boolean await(CompletableFuture<T> f) { return await(f, Long.MAX_VALUE); }
@Override public CompletableFuture<Void> process(TestEvent testEvent) { return Futures.failedFuture(exception); } }
private CompletableFuture<Void> delay() { val result = Futures.delayedFuture(this.config.getSegmentMetadataExpiration(), this.executor); this.stopToken.register(result); return result; } }
@Override protected CompletableFuture<Void> doRun() { return Futures.loop( () -> !this.stopToken.isCancellationRequested(), () -> delay().thenCompose(v -> runOnce()), this.executor); }
CompletableFuture<List<TableEntry>> getResultFutures() { return Futures.allOfWithResults(this.resultFutures); } }
/** * Attaches the given callback as an exception listener to the given CompletableFuture, which will be invoked when * the future times out (fails with a TimeoutException). * * @param future The future to attach to. * @param callback The callback to invoke. * @param <T> The Type of the future's result. */ public static <T> void onTimeout(CompletableFuture<T> future, Consumer<TimeoutException> callback) { exceptionListener(future, TimeoutException.class, callback); }
private void report() { ExecutorServiceHelpers.Snapshot s = ExecutorServiceHelpers.getSnapshot(this.executor); if (s != null) { this.queueSize.reportSuccessValue(s.getQueueSize()); this.activeThreads.reportSuccessValue(s.getActiveThreadCount()); } } }
TailRead(long afterSequenceNumber, int maxCount, Duration timeout, ScheduledExecutorService executor) { this.afterSequenceNumber = afterSequenceNumber; this.maxCount = maxCount; this.future = Futures.futureWithTimeout(timeout, executor); }
@Override public void run() { runOrFail(this, this.result); }
/** * Operates like Executors.cachedThreadPool but with a custom thread timeout and pool name. * @return A new threadPool * @param maxThreadCount The maximum number of threads to allow in the pool. * @param threadTimeout the number of milliseconds that a thread should sit idle before shutting down. * @param poolName The name of the threadpool. */ public static ThreadPoolExecutor getShrinkingExecutor(int maxThreadCount, int threadTimeout, String poolName) { return new ThreadPoolExecutor(0, maxThreadCount, threadTimeout, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), getThreadFactory(poolName), new CallerRuns()); // Caller runs only occurs after shutdown, as queue size is unbounded. }
@Override public void close() { ExecutorServiceHelpers.shutdown(this.executor); } }
/** * Creates a new CompletableFuture that will timeout after the given amount of time. * * @param timeout The timeout for the future. * @param executorService An ExecutorService that will be used to invoke the timeout on. * @param <T> The Type argument for the CompletableFuture to create. * @return A CompletableFuture with a timeout. */ public static <T> CompletableFuture<T> futureWithTimeout(Duration timeout, ScheduledExecutorService executorService) { return futureWithTimeout(timeout, null, executorService); }
private boolean canRead(CancellationToken cancellationToken) { if (cancellationToken.isCancellationRequested()) { return false; } synchronized (this) { return !this.closed; } }
@Override public void cleanupTaskStore() throws IOException { if (executor != null) { ExecutorServiceHelpers.shutdown(executor); } } }