Class ConcurrentRunner<T>
- java.lang.Object
-
- com.github.robtimus.junit.support.concurrent.ConcurrentRunner<T>
-
- Type Parameters:
T
- The type of result for instances.
public final class ConcurrentRunner<T> extends Object
A class that will run code concurrently.- Author:
- Rob Spoor
- Since:
- 3.0
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ConcurrentRunner<T>
concurrentlyWith(Executable executable)
Adds an executable to call concurrently.ConcurrentRunner<T>
concurrentlyWith(Executable executable, int count)
Adds an executable to call concurrently.ConcurrentRunner<T>
concurrentlyWith(ThrowingSupplier<? extends T> supplier)
Adds a supplier to call concurrently.ConcurrentRunner<T>
concurrentlyWith(ThrowingSupplier<? extends T> supplier, int count)
Adds a supplier to call concurrently.ConcurrentResults<T>
execute()
Calls all provided suppliers concurrently using the providednumber of threads
.static void
runConcurrently(List<Executable> executables)
Runs several blocks of code concurrently.static void
runConcurrently(Executable... executables)
Runs several blocks of code concurrently.static void
runConcurrently(Executable executable, int count)
Runs a block of code several times concurrently.static void
runConcurrently(Executable executable, ConcurrencySettings settings)
Runs a block of code several times concurrently.static ConcurrentRunner<Void>
running(Executable executable)
Creates a new concurrent runner.static ConcurrentRunner<Void>
running(Executable executable, int count)
Creates a new concurrent runner.static <T> ConcurrentRunner<T>
running(ThrowingSupplier<? extends T> supplier)
Creates a new concurrent runner.static <T> ConcurrentRunner<T>
running(ThrowingSupplier<? extends T> supplier, int count)
Creates a new concurrent runner.ConcurrentRunner<T>
withThreadCount(int threadCount)
Sets the number of threads to use.
-
-
-
Method Detail
-
running
public static <T> ConcurrentRunner<T> running(ThrowingSupplier<? extends T> supplier)
Creates a new concurrent runner.- Type Parameters:
T
- The type of result.- Parameters:
supplier
- The first supplier to call.- Returns:
- The created concurrent runner.
- Throws:
NullPointerException
- If the given supplier isnull
.
-
running
public static <T> ConcurrentRunner<T> running(ThrowingSupplier<? extends T> supplier, int count)
Creates a new concurrent runner.- Type Parameters:
T
- The type of result.- Parameters:
supplier
- The first supplier to call.count
- The number of times to call the supplier.- Returns:
- The created concurrent runner.
- Throws:
NullPointerException
- If the given supplier isnull
.IllegalArgumentException
- If the given count is not positive.
-
running
public static ConcurrentRunner<Void> running(Executable executable)
Creates a new concurrent runner. This is equivalent to callingrunning(ThrowingSupplier)
with a supplier that callsexecutable.execute()
and then returnsnull
.- Parameters:
executable
- The first executable to call.- Returns:
- This object.
- Throws:
NullPointerException
- If the given executable isnull
.
-
running
public static ConcurrentRunner<Void> running(Executable executable, int count)
Creates a new concurrent runner. This is equivalent to callingrunning(ThrowingSupplier, int)
with a supplier that callsexecutable.execute()
and then returnsnull
.- Parameters:
executable
- The first executable to call.count
- The number of times to call the executable.- Returns:
- This object.
- Throws:
NullPointerException
- If the given executable isnull
.IllegalArgumentException
- If the given count is not positive.
-
concurrentlyWith
public ConcurrentRunner<T> concurrentlyWith(ThrowingSupplier<? extends T> supplier)
Adds a supplier to call concurrently.- Parameters:
supplier
- The additional supplier to call.- Returns:
- This object.
- Throws:
NullPointerException
- If the given supplier isnull
.
-
concurrentlyWith
public ConcurrentRunner<T> concurrentlyWith(ThrowingSupplier<? extends T> supplier, int count)
Adds a supplier to call concurrently.- Parameters:
supplier
- The additional supplier to call.count
- The number of times to call the supplier.- Returns:
- This object.
- Throws:
NullPointerException
- If the given supplier isnull
.IllegalArgumentException
- If the given count is not positive.
-
concurrentlyWith
public ConcurrentRunner<T> concurrentlyWith(Executable executable)
Adds an executable to call concurrently. This is equivalent to callingconcurrentlyWith(ThrowingSupplier)
with a supplier that callsexecutable.execute()
and then returnsnull
.- Parameters:
executable
- The additional executable to call.- Returns:
- This object.
- Throws:
NullPointerException
- If the given executable isnull
.
-
concurrentlyWith
public ConcurrentRunner<T> concurrentlyWith(Executable executable, int count)
Adds an executable to call concurrently. This is equivalent to callingconcurrentlyWith(ThrowingSupplier, int)
with a supplier that callsexecutable.execute()
and then returnsnull
.- Parameters:
executable
- The additional executable to call.count
- The number of times to call the supplier.- Returns:
- This object.
- Throws:
NullPointerException
- If the given executable isnull
.IllegalArgumentException
- If the given count is not positive.
-
withThreadCount
public ConcurrentRunner<T> withThreadCount(int threadCount)
Sets the number of threads to use. By default each provided supplier will get its own thread.- Parameters:
threadCount
- The number of threads to use.- Returns:
- This object.
- Throws:
IllegalArgumentException
- If the given thread count is not at least 2.
-
execute
public ConcurrentResults<T> execute()
Calls all provided suppliers concurrently using the providednumber of threads
. If no thread count has been given a thread for each provided supplier will be used.Note that calling this method will not process any result until a method on the return value is called.
- Returns:
- The results of calling the suppliers.
-
runConcurrently
public static void runConcurrently(Executable executable, int count)
Runs a block of code several times concurrently. Each block of code will start at approximately the same time.This method assumes that each block of code does not throw any exception, including failed assertions. If any block of code does, this method will throw an error or exception.
- Parameters:
executable
- The block of code to run concurrently.count
- The number of times to run the block of code.- Throws:
NullPointerException
- If the given executable isnull
.IllegalArgumentException
- If the given count is not positive.
-
runConcurrently
public static void runConcurrently(Executable executable, ConcurrencySettings settings)
Runs a block of code several times concurrently. Each block of code will start at approximately the same time.This method assumes that each block of code does not throw any exception, including failed assertions. If any block of code does, this method will throw an error or exception.
- Parameters:
executable
- The block of code to run concurrently.settings
- The concurrency settings, including the number of times to run the block of code.- Throws:
NullPointerException
- If the given executable or settings object isnull
.
-
runConcurrently
public static void runConcurrently(Executable... executables)
Runs several blocks of code concurrently. Each block of code will start at approximately the same time.This method assumes that each block of code does not throw any exception, including failed assertions. If any block of code does, this method will throw an error or exception.
- Parameters:
executables
- The blocks of code to run concurrently.- Throws:
NullPointerException
- If any of the given executables isnull
.
-
runConcurrently
public static void runConcurrently(List<Executable> executables)
Runs several blocks of code concurrently. Each block of code will start at approximately the same time.This method assumes that each block of code does not throw any exception, including failed assertions. If any block of code does, this method will throw an error or exception.
- Parameters:
executables
- The blocks of code to run concurrently.- Throws:
NullPointerException
- If any of the given executables isnull
.
-
-