martin.common.compthreads
Class ArrayBasedMaster<E>
java.lang.Object
martin.common.compthreads.Master<E>
martin.common.compthreads.ArrayBasedMaster<E>
- All Implemented Interfaces:
- java.lang.Runnable
public class ArrayBasedMaster<E>
- extends Master<E>
- implements java.lang.Runnable
Class for facilitating parallelized computations.
This class provides functionality when the "problem" instances are stored in an array, while the IteratorBasedMaster class is better suited when the problems are given by an iterator.
Usage example: new Thread(new ArrayBasedMaster(problems, 4)).start(); Object[] solutions = master.getSolutions();
- Author:
- Martin
Method Summary |
java.util.ArrayList<E> |
getArrayListSolutions()
|
java.lang.Object[] |
getSolutions()
This method will block until all problems have been processed and computed. |
(package private) void |
report(E solution,
int id)
Called by the worker threads as they complete. |
void |
run()
The method which is run as the master thread starts. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
problems
private Problem<E>[] problems
solutions
private java.lang.Object[] solutions
threadsem
private java.util.concurrent.Semaphore threadsem
solutionsem
private java.util.concurrent.Semaphore solutionsem
ArrayBasedMaster
public ArrayBasedMaster(Problem<E>[] problems,
int numThreads)
run
public void run()
- The method which is run as the master thread starts. Should not be run directly by the user.
- Specified by:
run
in interface java.lang.Runnable
- Specified by:
run
in class Master<E>
getSolutions
public java.lang.Object[] getSolutions()
- This method will block until all problems have been processed and computed.
Returned solutions will be in the same order as the order of the problems given to the constructor.
- Returns:
- the solutions to the problems specified (i.e. an array with the objects that are returned from the problem compute() methods).
getArrayListSolutions
public java.util.ArrayList<E> getArrayListSolutions()
report
void report(E solution,
int id)
- Called by the worker threads as they complete.
- Specified by:
report
in class Master<E>