public class ExecutorNotifier extends Object implements AutoCloseable
Constructor and Description |
---|
ExecutorNotifier(ScheduledExecutorService workerExecutor,
Executor executorToNotify) |
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the executor notifier.
|
<T> void |
notifyReadyAsync(Callable<T> callable,
java.util.function.BiConsumer<T,Throwable> handler)
Call the given callable once.
|
<T> void |
notifyReadyAsync(Callable<T> callable,
java.util.function.BiConsumer<T,Throwable> handler,
long initialDelayMs,
long periodMs)
Call the given callable once.
|
public ExecutorNotifier(ScheduledExecutorService workerExecutor, Executor executorToNotify)
public <T> void notifyReadyAsync(Callable<T> callable, java.util.function.BiConsumer<T,Throwable> handler)
executorToNotify
to execute the handler.
Note that when this method is invoked multiple times, it is possible that multiple
callables are executed concurrently, so do the handlers. For example, assuming both the
workerExecutor and executorToNotify are single threaded. The following code may still throw a
ConcurrentModificationException
.
final List<Integer> list = new ArrayList<>();
// The callable adds an integer 1 to the list, while it works at the first glance,
// A ConcurrentModificationException may be thrown because the caller and
// handler may modify the list at the same time.
notifier.notifyReadyAsync(
() -> list.add(1),
(ignoredValue, ignoredThrowable) -> list.add(2));
Instead, the above logic should be implemented in as:
// Modify the state in the handler.
notifier.notifyReadyAsync(() -> 1, (v, ignoredThrowable) -> {
list.add(v));
list.add(2);
});
callable
- the callable to invoke before notifying the executor.handler
- the handler to handle the result of the callable.public <T> void notifyReadyAsync(Callable<T> callable, java.util.function.BiConsumer<T,Throwable> handler, long initialDelayMs, long periodMs)
executorToNotify
to execute the handler.
Note that when this method is invoked multiple times, it is possible that multiple
callables are executed concurrently, so do the handlers. For example, assuming both the
workerExecutor and executorToNotify are single threaded. The following code may still throw a
ConcurrentModificationException
.
final List<Integer> list = new ArrayList<>();
// The callable adds an integer 1 to the list, while it works at the first glance,
// A ConcurrentModificationException may be thrown because the caller and
// handler may modify the list at the same time.
notifier.notifyReadyAsync(
() -> list.add(1),
(ignoredValue, ignoredThrowable) -> list.add(2));
Instead, the above logic should be implemented in as:
// Modify the state in the handler.
notifier.notifyReadyAsync(() -> 1, (v, ignoredThrowable) -> {
list.add(v));
list.add(2);
});
callable
- the callable to execute before notifying the executor to notify.handler
- the handler that handles the result from the callable.initialDelayMs
- the initial delay in ms before invoking the given callable.periodMs
- the interval in ms to invoke the callable.public void close() throws InterruptedException
close
in interface AutoCloseable
InterruptedException
- when interrupted during closure.Copyright © 2014–2021 The Apache Software Foundation. All rights reserved.