Class Tasks
This class cannot be inherited nor instantiated.
Thread safety
Methods of this class are safe to be accessed from multiple threads concurrently.Synchronization transparency
Methods of this class are synchronization transparent.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Consumer<T>Returns aConsumerwhoseapplymethod does nothing but returns immediately to the caller.static RunnablenoOpTask()Returns aRunnablewhoserun()method does nothing but returns immediately to the caller.static voidrunConcurrently(Runnable... tasks) Executes the specified tasks concurrently, each on a separate thread, attempting to execute them as concurrently as possible.static voidrunConcurrently(Collection<? extends Runnable> tasks) Executes the specified tasks concurrently, each on a separate thread, attempting to execute them as concurrently as possible.static RunnablerunOnceTask(Runnable task) Returns aRunnablewhich will execute the specifiedRunnablebut will execute the specifiedRunnableonly once.static RunnablerunOnceTaskStrict(Runnable task) Returns aRunnablewhich will execute the specifiedRunnablebut will execute the specifiedRunnableonly once, failing on multiple run attempts.
-
Method Details
-
noOpConsumer
Returns aConsumerwhoseapplymethod does nothing but returns immediately to the caller.- Type Parameters:
T- the type of the objects the returned consumer processes- Returns:
- a
Consumerwhoseapplymethod does nothing but returns immediately to the caller. This method never returnsnull.
-
noOpTask
Returns aRunnablewhoserun()method does nothing but returns immediately to the caller.- Returns:
- a
Runnablewhoserun()method does nothing but returns immediately to the caller. This method never returnsnull.
-
runOnceTask
Returns aRunnablewhich will execute the specifiedRunnablebut will execute the specifiedRunnableonly once. The specified task will not be executed more than once even if it is called multiple times concurrently (and is allowed to be called concurrently).Calling the
runmethod of the returnedRunnablemultiple times will only result in a single execution and every other call (not actually executing the specifiedRunnable) will silently return without doing anything.- Parameters:
task- theRunnableto which calls are to be forwarded by the returnedRunnable. This method cannot benull.- Returns:
- the
Runnablewhich will execute the specifiedRunnablebut will execute the specifiedRunnableonly once. This method never returnsnull.
-
runOnceTaskStrict
Returns aRunnablewhich will execute the specifiedRunnablebut will execute the specifiedRunnableonly once, failing on multiple run attempts. The specified task will not be executed more than once even if it is called multiple times concurrently (and is allowed to be called concurrently).Attempting to call the
runmethod of the returnedRunnablemultiple times will cause anIllegalStateExceptionto be thrown after the first attempt.- Parameters:
task- theRunnableto which calls are to be forwarded by the returnedRunnable. This method cannot benull.- Returns:
- the
Runnablewhich will execute the specifiedRunnablebut will execute the specifiedRunnableonly once. This method never returnsnull.
-
runConcurrently
Executes the specified tasks concurrently, each on a separate thread, attempting to execute them as concurrently as possible. This method was designed for test codes wanting to test the behaviour of multiple tasks if run concurrently. This method will attempt to start the passed tasks in sync (this does not imply thread-safety guarantees), so that there is a better chance that they actually run concurrently.This method will wait until all the specified tasks complete.
Warning: This method was not designed to give better performance by running the tasks concurrently. Performance of this method is secondary to any other purposes.
- Parameters:
tasks- the tasks to be run concurrently. Each of the specified tasks will run on a dedicated thread. This argument and its elements cannot benull.- Throws:
NullPointerException- thrown if the argument or any of the specified tasks isnullTaskExecutionException- thrown if any of the tasks thrown an exception. The first exception (in the order they were passed) is the cause of this exception and subsequent exceptions are suppressed (viaThrowable.addSuppressed).
-
runConcurrently
Executes the specified tasks concurrently, each on a separate thread, attempting to execute them as concurrently as possible. This method was designed for test codes wanting to test the behaviour of multiple tasks if run concurrently. This method will attempt to start the passed tasks in sync (this does not imply thread-safety guarantees), so that there is a better chance that they actually run concurrently.This method will wait until all the specified tasks complete.
Warning: This method was not designed to give better performance by running the tasks concurrently. Performance of this method is secondary to any other purposes.
- Parameters:
tasks- the tasks to be run concurrently. Each of the specified tasks will run on a dedicated thread. This argument and its elements cannot benull.- Throws:
NullPointerException- thrown if the argument or any of the specified tasks isnullTaskExecutionException- thrown if any of the tasks thrown an exception. The first exception (in the order they were passed) is the cause of this exception and subsequent exceptions are suppressed (viaThrowable.addSuppressed).
-