Class MultiThreadedTask<ResultType>
- java.lang.Object
-
- com.biomatters.geneious.publicapi.utilities.MultiThreadedTask<ResultType>
-
public abstract class MultiThreadedTask<ResultType> extends java.lang.Object
Manages a task that can be split over multiple threads.
Example 1: to split a task across multiple threads (1 per processor), use:List<ResultFromSingleThread> results = new MultiThreadedTask<ResultFromSingleThread>() {
- Since:
- API 4.610 (Geneious 6.1.0)
- See Also:
MultiThreadedProcessingSystem
-
-
Constructor Summary
Constructors Constructor Description MultiThreadedTask()
Creates a task split over a number of threads equal to the number of processors available on this machine.MultiThreadedTask(int numberOfThreads)
Creates a task split over the specified number of threads
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static int
getNumberOfProcessors()
static int
getNumberOfProcessorsSlightlyReducedIfLotsOfProcessors()
boolean
hasAnyPartThrownAnException()
java.util.List<ResultType>
run()
Creates threads, executes all the parts and waits for them to finish before returning.abstract ResultType
run(int partNumber, int numberOfParts)
Runs a part of this task in a single thread.
-
-
-
Constructor Detail
-
MultiThreadedTask
public MultiThreadedTask()
Creates a task split over a number of threads equal to the number of processors available on this machine.
-
MultiThreadedTask
public MultiThreadedTask(int numberOfThreads)
Creates a task split over the specified number of threads- Parameters:
numberOfThreads
- the number of threads
-
-
Method Detail
-
getNumberOfProcessors
public static int getNumberOfProcessors()
- Returns:
- the number of processors on this machine. You should probably use
getNumberOfProcessorsSlightlyReducedIfLotsOfProcessors()
instead if the task may allocate temporary memory.
-
getNumberOfProcessorsSlightlyReducedIfLotsOfProcessors
public static int getNumberOfProcessorsSlightlyReducedIfLotsOfProcessors()
- Returns:
- the number of processors on this machine. Equivalent to Runtime.getRuntime().availableProcessors() unless there are at least 16 processors, in which case this value may be reduced by 1 or 2 to give the JVM garbage collector a chance to run
- Since:
- API 4.1011 (Geneious 10.1.1)
-
run
public abstract ResultType run(int partNumber, int numberOfParts)
Runs a part of this task in a single thread.- Parameters:
partNumber
- the part number of this part of the task between 0 and numberOfParts-1 inclusive.numberOfParts
- the total number of parts this task has been split into.- Returns:
- The results of this part.
-
run
public java.util.List<ResultType> run()
Creates threads, executes all the parts and waits for them to finish before returning. If at least one thread throws an exception, only the first exception caught by the thread manager in this method will will be thrown by this method.- Returns:
- the results of all parts ordered by the partNumbers.
-
hasAnyPartThrownAnException
public boolean hasAnyPartThrownAnException()
- Returns:
- true if any of the threads/parts running this task have thrown an exception which has been caught by the thread manager in
run()
. It is recommended that each thread should periodically call this and terminate if this returns true. This is useful to ensure the task promptly fails when any part of the task crashes rather than delaying the crash propagation until all parts have finished. If the calling thread chooses to throw an exception when this returns true, that exception will not be propagated back torun()
because only the first exception caught by the thread manager inrun()
will be thrown fromrun()
- Since:
- API 4.202230 (Geneious 2022.3.0)
-
-