public class ParallelismTools
extends Object
Modifier and Type | Field and Description |
---|---|
static String |
RW_PARALLEL
Name of the environment variable to disable parallel processing, set RW_PARALLEL=0 to disable
|
Constructor and Description |
---|
ParallelismTools() |
Modifier and Type | Method and Description |
---|---|
static <T> RunnableFuture<T> |
adapt(Callable<T> task)
Adapt a task-with-return value into a RunnableFuture object that implements
the Future interface to be executed by the current thread (as opposed to
submitting it to thread pool queue).
|
static <T> T |
get(Future<T> future)
Block until the task behind the given Future is complete.
|
static boolean |
getParallel()
Global getter for current parallel processing state.
|
static <T> List<Future<T>> |
invokeAll(Callable<T>... tasks)
Given a list of tasks-with-return-value, block until all tasks
have been completed.
|
static <T,R> List<Future<R>> |
invokeAll(Collection<T> items,
Function<T,R> task)
Run the specified task on all items
|
static void |
invokeAll(Runnable... tasks)
Given a list of tasks-without-return-value, block until all tasks
have been completed.
|
static <T> void |
invokeAllRunnable(Collection<T> items,
Consumer<T> task)
Run the specified task on all items
|
static <T> Deque<Future<T>> |
invokeFirstSubmitRest(Callable<T>... tasks)
For a given list of value-returning-tasks, first submit all but the first task to
the thread pool to be executed in parallel, then execute that first task with the
current thread.
|
static <T> void |
join(List<? extends Future<? extends T>> futures)
For a given List of Futures, block until all are complete.
|
static <T> T |
joinFirst(Deque<Future<T>> futures)
For a given Deque of Futures, block until the first is complete and
remove it from the deque.
|
static int |
maxParallelism()
The number of parallel threads we want to use
|
static void |
setParallel(boolean parallel)
Global setter to control parallel processing.
|
static <T> Future<T> |
submit(Callable<T> task)
Submit a task-with-return-value to the thread pool.
|
static Future<?> |
submit(Runnable task)
Submit a task-without-return-value to the thread pool.
|
public static final String RW_PARALLEL
public static void setParallel(boolean parallel)
parallel
- Enable parallel processing.public static boolean getParallel()
public static <T> Future<T> submit(Callable<T> task)
T
- Type returned by task.task
- Task to be performed.public static Future<?> submit(Runnable task)
task
- Task to be performed.public static <T> T get(Future<T> future)
future
- Future representing previously submitted task.@SafeVarargs public static <T> Deque<Future<T>> invokeFirstSubmitRest(@NotNull Callable<T>... tasks)
T
- Type returned by all tasks.tasks
- List of tasks to be executed.public static <T> T joinFirst(Deque<Future<T>> futures)
T
- Type returned by all tasks.futures
- A Deque of Future objects corresponding to previously
submitted tasks.public static <T> void join(List<? extends Future<? extends T>> futures)
T
- Type returned by all tasks.futures
- A List of Future objects corresponding to previously
submitted tasks.public static <T> void invokeAllRunnable(Collection<T> items, Consumer<T> task)
T
- item typeitems
- the items to call the task withtask
- the task that should be executed for all itemspublic static void invokeAll(@NotNull Runnable... tasks)
tasks
- List of tasks-without-return-value.public static <T,R> List<Future<R>> invokeAll(Collection<T> items, Function<T,R> task)
T
- item typeitems
- the items to call the task withtask
- the task that should be executed for all itemspublic static <T> List<Future<T>> invokeAll(Callable<T>... tasks)
T
- Type returned by all tasks.tasks
- List of tasks-with-return-value.public static <T> RunnableFuture<T> adapt(Callable<T> task)
T
- Type returned by task.task
- Task with return value.public static int maxParallelism()