Class Async
- java.lang.Object
-
- org.kiwiproject.concurrent.Async
-
public class Async extends Object
Static utilities that work withCompletableFutureand can make testing easier by permitting selective (e.g. in unit tests) forcing of synchronous behavior for things that would normally execute asynchronously. This applies only to some methods, so read the method's documentation before assuming.Use the
xxxAsyncmethods when you need to control concurrent behavior and make things deterministic during unit tests (e.g. blocking on futures). Note this does actually change the true behavior of the code under test, since methods will execute synchronously, so use with care, caution, and understanding.- Implementation Note:
- the "asyncMode" flag is a STATIC variable and should only ever be changed during testing using the
setUnitTestAsyncMode(Mode)method. Generally, you should set this before tests and reset after they have run. Note also this almost certainly will cause unexpected behavior if tests are run in parallel.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAsync.ModeTheAsync.Mode.ENABLEDmode enables asynchronous behavior, which is the default behavior.
-
Constructor Summary
Constructors Constructor Description Async()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static CompletableFuture<Void>doAsync(Runnable func)Execute the givenRunnableasynchronously.static CompletableFuture<Void>doAsync(Runnable func, Executor executor)static <T> CompletableFuture<T>doAsync(Supplier<T> supplier)Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.static <T> CompletableFuture<T>doAsync(Supplier<T> supplier, Executor executor)Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.static CompletableFuture<Void>runAsync(Runnable func)Execute the givenRunnableasynchronously.static CompletableFuture<Void>runAsync(Runnable func, Executor executor)static voidsetUnitTestAsyncMode(Async.Mode mode)Use for testing purposes to force synchronous behavior.static <T> CompletableFuture<T>supplyAsync(Supplier<T> supplier)Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.static <T> CompletableFuture<T>supplyAsync(Supplier<T> supplier, Executor executor)Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.static <T> voidwaitFor(CompletableFuture<T> future, long timeout, TimeUnit unit)Helper method that waits for aCompletableFutureup to a specified timeout.static <T> voidwaitForAll(Collection<CompletableFuture<T>> futures, long timeout, TimeUnit unit)Helper method that waits for a collection ofCompletableFutureof typeTup to a specified timeout.static voidwaitForAllIgnoringType(Collection<CompletableFuture> futures, long timeout, TimeUnit unit)Helper method that waits for a collection ofCompletableFuturewith no explicit type up to a specified timeout.static <T> CompletableFuture<T>withMaxTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit)Wraps aCompletableFuturewith a timeout so that it can proceed asynchronously, but still have a maximum duration.static <T> CompletableFuture<T>withMaxTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit, ExecutorService executor)Wraps aCompletableFuturewith a timeout so that it can proceed asynchronously, but still have a maximum duration.
-
-
-
Method Detail
-
setUnitTestAsyncMode
public static void setUnitTestAsyncMode(Async.Mode mode)
Use for testing purposes to force synchronous behavior.- Parameters:
mode- enable or disable asynchronous behavior
-
runAsync
public static CompletableFuture<Void> runAsync(Runnable func)
Execute the givenRunnableasynchronously. This uses the common fork join pool as the executor.This is an alias method for
doAsync(Runnable)to provide a way to avoid ambiguity in certain situations.- Parameters:
func- the code to run asynchronously- Returns:
- a
CompletableFuturewith no result - See Also:
ForkJoinPool.commonPool(),runAsync(Runnable, Executor)
-
runAsync
public static CompletableFuture<Void> runAsync(Runnable func, Executor executor)
Execute the givenRunnableasynchronously using the givenExecutor.Essentially, wraps
CompletableFuture.runAsync(Runnable, Executor)but allowing synchronous behavior if mode isAsync.Mode.DISABLED.This is an alias method for
doAsync(Runnable, Executor)to provide a way to avoid ambiguity in certain situations.- Parameters:
func- the code to run asynchronouslyexecutor- theExecutorto use- Returns:
- a
CompletableFuturewith no result - See Also:
CompletableFuture.runAsync(Runnable, Executor)
-
doAsync
public static CompletableFuture<Void> doAsync(Runnable func)
Execute the givenRunnableasynchronously. This uses the common fork join pool as the executor.- Parameters:
func- the code to run asynchronously- Returns:
- a
CompletableFuturewith no result - See Also:
ForkJoinPool.commonPool(),doAsync(Runnable, Executor)
-
doAsync
public static CompletableFuture<Void> doAsync(Runnable func, Executor executor)
Execute the givenRunnableasynchronously using the givenExecutor.Essentially, wraps
CompletableFuture.runAsync(Runnable, Executor)but allowing synchronous behavior if mode isAsync.Mode.DISABLED.- Parameters:
func- the code to run asynchronouslyexecutor- theExecutorto use- Returns:
- a
CompletableFuturewith no result - See Also:
CompletableFuture.runAsync(Runnable, Executor)
-
supplyAsync
public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier)
Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.This is an alias method for
doAsync(Supplier)to provide a way to avoid ambiguity in certain situations.- Type Parameters:
T- the type of object being supplied- Parameters:
supplier- the code to run asynchronously- Returns:
- the result returned by the supplier
- See Also:
ForkJoinPool.commonPool(),doAsync(Supplier, Executor)
-
supplyAsync
public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier, Executor executor)
Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.Essentially, wraps
CompletableFuture.supplyAsync(Supplier, Executor)but allowing synchronous behavior if mode isAsync.Mode.DISABLED.This is an alias method for
doAsync(Supplier, Executor)to provide a way to avoid ambiguity in certain situations.- Type Parameters:
T- the type of object being supplied- Parameters:
supplier- the code to run asynchronouslyexecutor- theExecutorto use- Returns:
- the result returned by the supplier
- See Also:
ForkJoinPool.commonPool(),CompletableFuture.supplyAsync(Supplier, Executor)
-
doAsync
public static <T> CompletableFuture<T> doAsync(Supplier<T> supplier)
Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.- Type Parameters:
T- the type of object being supplied- Parameters:
supplier- the code to run asynchronously- Returns:
- the result returned by the supplier
- See Also:
ForkJoinPool.commonPool(),doAsync(Supplier, Executor)
-
doAsync
public static <T> CompletableFuture<T> doAsync(Supplier<T> supplier, Executor executor)
Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.Essentially, wraps
CompletableFuture.supplyAsync(Supplier, Executor)but allowing synchronous behavior if mode isAsync.Mode.DISABLED.- Type Parameters:
T- the type of object being supplied- Parameters:
supplier- the code to run asynchronouslyexecutor- theExecutorto use- Returns:
- the result returned by the supplier
- See Also:
ForkJoinPool.commonPool(),CompletableFuture.supplyAsync(Supplier, Executor)
-
waitFor
public static <T> void waitFor(CompletableFuture<T> future, long timeout, TimeUnit unit)
Helper method that waits for aCompletableFutureup to a specified timeout.Note that
Async.Modehas no effect on this method.- Type Parameters:
T- the result returned by the future- Parameters:
future- the CompletableFuture to wait fortimeout- the value of the timeout in the given unitunit- the time unit to use- Throws:
AsyncException- if any error occurs during asynchronous code execution
-
waitForAll
public static <T> void waitForAll(Collection<CompletableFuture<T>> futures, long timeout, TimeUnit unit)
Helper method that waits for a collection ofCompletableFutureof typeTup to a specified timeout.Note that
Async.Modehas no effect on this method.- Type Parameters:
T- the result returned by the futures- Parameters:
futures- the CompletableFuture instances to wait fortimeout- the value of the timeout in the given unitunit- the time unit to use
-
waitForAllIgnoringType
public static void waitForAllIgnoringType(Collection<CompletableFuture> futures, long timeout, TimeUnit unit)
Helper method that waits for a collection ofCompletableFuturewith no explicit type up to a specified timeout.Note that
Async.Modehas no effect on this method.- Parameters:
futures- the CompletableFuture instances to wait fortimeout- the value of the timeout in the given unitunit- the time unit to use- Throws:
AsyncException- if any error occurs during asynchronous code execution- Implementation Note:
- Suppressed the IntelliJ and Sonar warnings about raw types
-
withMaxTimeout
public static <T> CompletableFuture<T> withMaxTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit)
Wraps aCompletableFuturewith a timeout so that it can proceed asynchronously, but still have a maximum duration. Uses the common fork join pool as theExecutorService.Note that
Async.Modehas no effect on this method.- Type Parameters:
T- the result returned by the future- Parameters:
future- the CompletableFuture for which to apply the timeouttimeout- the value of the timeout in the given unitunit- the time unit to use- Returns:
- the original
CompletableFuturewrapped by a new one that applies the given timeout - Throws:
AsyncException- if any error occurs during asynchronous code execution, including timeout- See Also:
withMaxTimeout(CompletableFuture, long, TimeUnit, ExecutorService)
-
withMaxTimeout
public static <T> CompletableFuture<T> withMaxTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit, ExecutorService executor)
Wraps aCompletableFuturewith a timeout so that it can proceed asynchronously, but still have a maximum duration. Uses the givenExecutorService.Note that
Async.Modehas no effect on this method.- Type Parameters:
T- the result returned by the future- Parameters:
future- the CompletableFuture for which to apply the timeouttimeout- the value of the timeout in the given unitunit- the time unit to useexecutor- theExecutorServiceto use- Returns:
- the original
CompletableFuturewrapped by a new one that applies the given timeout - Throws:
AsyncException- if any error occurs during asynchronous code execution, including timeout- See Also:
CompletableFuture.supplyAsync(Supplier, Executor)
-
-