public class TestThreadsHolder<T extends java.lang.Runnable> extends java.lang.Object implements RunnerCreator<java.lang.Runnable>
Thread.interrupt().| Constructor and Description |
|---|
TestThreadsHolder(int numberOfThreads)
Creates a specified number of threads that will perform the given runnable logic as soon as
startAll()
has been invoked. |
TestThreadsHolder(int numberOfThreads,
boolean inheritTenant)
Creates a specified number of threads that will perform the given runnable logic as soon as
startAll()
has been invoked. |
TestThreadsHolder(int numberOfThreads,
RunnerCreator<T> creator)
Creates a specified number of threads that will perform the logic created by the specified runnable creator as
soon as
startAll() has been invoked. |
TestThreadsHolder(int numberOfThreads,
RunnerCreator<T> creator,
boolean inheritTenant)
Creates a specified number of threads that will perform the logic created by the specified runnable creator as
soon as
startAll() has been invoked. |
TestThreadsHolder(int numberOfThreads,
T runnable)
Creates a specified number of threads that will perform the given runnable logic as soon as
startAll()
has been invoked. |
TestThreadsHolder(int numberOfThreads,
T runnable,
boolean inheritTenant)
Creates a specified number of threads that will perform the given runnable logic as soon as
startAll()
has been invoked. |
| Modifier and Type | Method and Description |
|---|---|
int |
getAlive()
Returns the current number of threads that did not complete their testing logic.
|
java.util.Map<java.lang.Integer,java.lang.Throwable> |
getErrors()
Returns all errors recorded from runner threads.
|
T |
getRunner(int pos) |
java.util.List<T> |
getRunners() |
long |
getStartToFinishMillis()
If all threads finished in
|
boolean |
hasErrors()
Tells whether any runner has recorded a error.
|
java.lang.Runnable |
newRunner(int threadNumber)
Creates a new runnable object for the given runner thread number.
|
boolean |
runAll(long duration,
java.util.concurrent.TimeUnit unit,
int stopWaitSeconds)
Starts all threads via
startAll() and lets them run for the specified duration. |
void |
startAll()
Allows all runner threads to perform the given test logic.
|
void |
stopAll()
Stops all runner threads by invoking
Thread.interrupt(). |
boolean |
stopAndDestroy(int timeWaitSeconds)
Attempts to stop all runners via
stopAll() and waits for completion for 10 seconds. |
protected boolean |
useBusyWaiting()
Override to change latch timeout guarded waiting method.
|
boolean |
waitAndDestroy(int terminationWaitSeconds)
Waits for runner termination for the specified time.
|
boolean |
waitForAll(long timeout,
java.util.concurrent.TimeUnit unit)
Waits for all runners to finish their work for a specified amount of time.
|
boolean |
waitForPrepared(long timeout,
java.util.concurrent.TimeUnit unit) |
public TestThreadsHolder(int numberOfThreads)
startAll()
has been invoked.
Please note that this constructor is intended for creating inner classes only. The newRunner(int)
method of this inner class *must* be overwritten!public TestThreadsHolder(int numberOfThreads,
boolean inheritTenant)
startAll()
has been invoked.
Please note that this constructor is intended for creating inner classes only. The newRunner(int)
method of this inner class *must* be overwritten!public TestThreadsHolder(int numberOfThreads,
T runnable)
startAll()
has been invoked.
Please note that technically the actual threads will be immediately created and started. Calling
startAll() only releases them from a waiting state.numberOfThreads - the number of runner threads to userunnable - the runnable instance being used by each runner threadpublic TestThreadsHolder(int numberOfThreads,
T runnable,
boolean inheritTenant)
startAll()
has been invoked.
Please note that technically the actual threads will be immediately created and started. Calling
startAll() only releases them from a waiting state.numberOfThreads - the number of runner threads to userunnable - the runnable instance being used by each runner threadinheritTenant - if true all runner threads will inherit the current callers tenant, if false no thread will have a
active tenantpublic TestThreadsHolder(int numberOfThreads,
RunnerCreator<T> creator)
startAll() has been invoked.
Please note that technically the actual threads will be immediately created and started. Calling
startAll() only releases them from a waiting state.numberOfThreads - the number of runner threads to usecreator - the creator responsible for providing a runnable instance for each runner threadpublic TestThreadsHolder(int numberOfThreads,
RunnerCreator<T> creator,
boolean inheritTenant)
startAll() has been invoked.
Please note that technically the actual threads will be immediately created and started. Calling
startAll() only releases them from a waiting state.numberOfThreads - the number of runner threads to usecreator - the creator responsible for providing a runnable instance for each runner threadinheritTenant - if true each runner thread will inherit the current callers tenant, if false all threads will have no
active tenant setprotected boolean useBusyWaiting()
CountDownLatch.await(long, TimeUnit) which is most efficient for not
disturbing workers and returning as quick as possible.
However if you're suffering from JVM being blocked often (not getting computing time for minutes) for
instance when running in a virtual environment it may be better to change timeout method to busily counting
'ticks'. In that case the latch is tested many times waiting at most one second. In case the JVM is being blocked
for a longer time the timeout mechanism simply continues where it was blocked giving workers a fair chance to
finished their work, even if it means that the actual timeout period is greatly exceeded!public java.lang.Runnable newRunner(int threadNumber)
RunnerCreatornewRunner in interface RunnerCreator<java.lang.Runnable>public boolean waitForPrepared(long timeout,
java.util.concurrent.TimeUnit unit)
public void startAll()
waitForAll(long, TimeUnit) or stop them
after some time via stopAll()stopAll(),
waitForAll(long, TimeUnit)public boolean runAll(long duration,
java.util.concurrent.TimeUnit unit,
int stopWaitSeconds)
startAll() and lets them run for the specified duration.
After that all threads are stopped via stopAndDestroy(int) and the stop result is being returned.duration - the time to rununit - the unit of time to runstopWaitSeconds - the time to wait after stopping threads before killing them 'the hard way'public void stopAll()
Thread.interrupt().
Please make sure that the tested logic is handling thread interrupts correctly, which means either to pass on
the InterruptedException or restoring the interrupted flag via Thread.interrupt()!public boolean stopAndDestroy(int timeWaitSeconds)
stopAll() and waits for completion for 10 seconds. If for some reason
this doesn't work it will unsafely stop all runners via Thread.stop().public boolean waitAndDestroy(int terminationWaitSeconds)
Thread.stop().terminationWaitSeconds - the time to wait for runner thread termination in seconds; after this time has elapsed it will unsafely
kill them via Thread.stop()public long getStartToFinishMillis()
public int getAlive()
public boolean waitForAll(long timeout,
java.util.concurrent.TimeUnit unit)
public boolean hasErrors()
getErrors()public java.util.Map<java.lang.Integer,java.lang.Throwable> getErrors()
InterruptedException will not be recorded since this we prefer thread interrupt as
valid way of stopping runners.hasErrors()public java.util.List<T> getRunners()
public T getRunner(int pos)
Copyright © 2018 SAP SE. All Rights Reserved.