Class MoreFutures
- java.lang.Object
-
- com.github.phantomthief.concurrent.MoreFutures
-
public class MoreFutures extends Object
- Author:
- w.vela Created on 2018-06-25.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMoreFutures.Scheduled
-
Constructor Summary
Constructors Constructor Description MoreFutures()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TgetUnchecked(Future<? extends T> future, long timeout, TimeUnit unit)static <T> TgetUnchecked(Future<? extends T> future, long timeout, TimeUnit unit, boolean cancelOnTimeout)static <T> TgetUnchecked(Future<? extends T> future, Duration duration)static Future<?>scheduleWithDynamicDelay(ScheduledExecutorService executor, Duration initDelay, MoreFutures.Scheduled task)static Future<?>scheduleWithDynamicDelay(ScheduledExecutorService executor, Supplier<Duration> delay, ThrowableRunnable<Throwable> task)static <I,O>
com.google.common.util.concurrent.ListenableFuture<O>transform(com.google.common.util.concurrent.ListenableFuture<I> input, Function<? super I,? extends O> function, Executor executor)用于替换Futures.transform(ListenableFuture, com.google.common.base.Function, Executor)static <F extends Future<V>,V>
Map<F,V>tryWait(Iterable<F> futures, long timeout, TimeUnit unit)A typical usage:<pre> // a fail-safe example List<Future<User>> list = doSomeAsyncTasks(); Map<Future<User>, User> success; try { success = tryWait(list, 1, SECONDS); } catch (TryWaitUncheckedException e) { success = e.getSuccess(); // there are still some success } // a fail-fast example List<Future<User>> list = doSomeAsyncTasks(); // don't try/catch the exception it throws. Map<Future<User>, User> success = tryWait(list, 1, SECONDS); </pre>static <F extends Future<V>,V>
Map<F,V>tryWait(Iterable<F> futures, Duration duration)static <K,V,X extends Throwable>
Map<K,V>tryWait(Iterable<K> keys, long timeout, TimeUnit unit, ThrowableFunction<K,Future<V>,X> asyncFunc)A typical usage:<pre> // a fail-safe example List<Integer> list = getSomeIds(); Map<Integer, User> success; try { success = tryWait(list, 1, SECONDS, id -> executor.submit(() -> retrieve(id))); } catch (TryWaitUncheckedException e) { success = e.getSuccess(); // there are still some success } // a fail-fast example List<Integer> list = getSomeIds(); // don't try/catch the exception it throws. Map<Integer, User> success = tryWait(list, 1, SECONDS, id -> executor.submit(() -> retrieve(id))); </pre>static <K,V,X extends Throwable>
Map<K,V>tryWait(Iterable<K> keys, Duration duration, ThrowableFunction<K,Future<V>,X> asyncFunc)
-
-
-
Method Detail
-
getUnchecked
public static <T> T getUnchecked(@Nonnull Future<? extends T> future, @Nonnull Duration duration)
- Throws:
com.google.common.util.concurrent.UncheckedTimeoutException- if timeout occurred.CancellationException- if task was canceled.com.google.common.util.concurrent.ExecutionError- if aErroroccurred.com.google.common.util.concurrent.UncheckedExecutionException- if a normal Exception occurred.
-
getUnchecked
public static <T> T getUnchecked(@Nonnull Future<? extends T> future, @Nonnegative long timeout, @Nonnull TimeUnit unit)
- Throws:
com.google.common.util.concurrent.UncheckedTimeoutException- if timeout occurred.CancellationException- if task was canceled.com.google.common.util.concurrent.ExecutionError- if aErroroccurred.com.google.common.util.concurrent.UncheckedExecutionException- if a normal Exception occurred.
-
getUnchecked
public static <T> T getUnchecked(@Nonnull Future<? extends T> future, @Nonnegative long timeout, @Nonnull TimeUnit unit, boolean cancelOnTimeout)
- Throws:
com.google.common.util.concurrent.UncheckedTimeoutException- if timeout occurred.CancellationException- if task was canceled.com.google.common.util.concurrent.ExecutionError- if aErroroccurred.com.google.common.util.concurrent.UncheckedExecutionException- if a normal Exception occurred.
-
tryWait
@Nonnull public static <F extends Future<V>,V> Map<F,V> tryWait(@Nonnull Iterable<F> futures, @Nonnull Duration duration) throws TryWaitFutureUncheckedException
- Throws:
TryWaitFutureUncheckedException- if not all calls are successful.- See Also:
tryWait(Iterable, long, TimeUnit)
-
tryWait
@Nonnull public static <F extends Future<V>,V> Map<F,V> tryWait(@Nonnull Iterable<F> futures, @Nonnegative long timeout, @Nonnull TimeUnit unit) throws TryWaitFutureUncheckedException
A typical usage:<pre> // a fail-safe example List<Future<User>> list = doSomeAsyncTasks(); Map<Future<User>, User> success; try { success = tryWait(list, 1, SECONDS); } catch (TryWaitUncheckedException e) { success = e.getSuccess(); // there are still some success } // a fail-fast example List<Future<User>> list = doSomeAsyncTasks(); // don't try/catch the exception it throws. Map<Future<User>, User> success = tryWait(list, 1, SECONDS); </pre>- Throws:
TryWaitUncheckedException- if not all calls are successful.TryWaitFutureUncheckedException
-
tryWait
@Nonnull public static <K,V,X extends Throwable> Map<K,V> tryWait(@Nonnull Iterable<K> keys, @Nonnull Duration duration, @Nonnull ThrowableFunction<K,Future<V>,X> asyncFunc) throws X extends Throwable, TryWaitUncheckedException
- Throws:
TryWaitUncheckedException- if not all calls are successful.X extends Throwable- See Also:
tryWait(Iterable, long, TimeUnit, ThrowableFunction)
-
tryWait
@Nonnull public static <K,V,X extends Throwable> Map<K,V> tryWait(@Nonnull Iterable<K> keys, @Nonnegative long timeout, @Nonnull TimeUnit unit, @Nonnull ThrowableFunction<K,Future<V>,X> asyncFunc) throws X extends Throwable, TryWaitUncheckedException
A typical usage:<pre> // a fail-safe example List<Integer> list = getSomeIds(); Map<Integer, User> success; try { success = tryWait(list, 1, SECONDS, id -> executor.submit(() -> retrieve(id))); } catch (TryWaitUncheckedException e) { success = e.getSuccess(); // there are still some success } // a fail-fast example List<Integer> list = getSomeIds(); // don't try/catch the exception it throws. Map<Integer, User> success = tryWait(list, 1, SECONDS, id -> executor.submit(() -> retrieve(id))); </pre>- Throws:
TryWaitUncheckedException- if not all calls are successful.X extends Throwable
-
scheduleWithDynamicDelay
public static Future<?> scheduleWithDynamicDelay(@Nonnull ScheduledExecutorService executor, @Nullable Duration initDelay, @Nonnull MoreFutures.Scheduled task)
- Parameters:
task- any exception throwing would cancel the task. user should swallow exceptions by self.executor- all task would be stopped after executor has been marked shutting down.- Returns:
- a future that can cancel the task.
-
scheduleWithDynamicDelay
public static Future<?> scheduleWithDynamicDelay(@Nonnull ScheduledExecutorService executor, @Nonnull Supplier<Duration> delay, @Nonnull ThrowableRunnable<Throwable> task)
- Parameters:
task- any exception throwing would be ignore and logged, task would not cancelled.executor- all task would be stopped after executor has been marked shutting down.- Returns:
- a future that can cancel the task.
-
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, Function<? super I,? extends O> function, Executor executor)用于替换Futures.transform(ListenableFuture, com.google.common.base.Function, Executor)主要提供两个额外的功能: 1. API使用jdk8 2. 提供了
TimeoutListenableFuture的支持(保持Listener不会丢)
-
-