martin.common.compthreads
Class IteratorBasedMaster<E>

java.lang.Object
  extended by martin.common.compthreads.Master<E>
      extended by martin.common.compthreads.IteratorBasedMaster<E>
Type Parameters:
E - The class representing solutions to computational problems
All Implemented Interfaces:
java.lang.Iterable<E>, java.lang.Runnable, java.util.Iterator<E>

public class IteratorBasedMaster<E>
extends Master<E>
implements java.lang.Runnable, java.util.Iterator<E>, java.lang.Iterable<E>

Class for facilitating parallelized computations. This class provides functionality when the "problem" instances are given by an iterator, while the ArrayBasedMaster class is better suited when the problems are stored in an array. Solutions can be retrieved as they complete through an iterator. Usage example: new Thread(new ArrayBasedMaster(problems, 4)).start(); Object[] solutions = master.getSolutions();

Author:
Martin

Field Summary
private  java.util.concurrent.Semaphore addedsem
           
private  int nextJobToRelease
           
private  java.util.Iterator<Problem<E>> problemIterator
           
private  java.util.Map<java.lang.Integer,E> solutions
           
private  java.util.concurrent.Semaphore solutionsem
           
private  java.util.Map<java.lang.Integer,java.util.concurrent.Semaphore> solutionSemaphores
           
private  java.util.concurrent.Semaphore storageLimitSem
           
private  java.util.concurrent.Semaphore threadsem
           
 
Constructor Summary
IteratorBasedMaster(java.util.Collection<Problem<E>> collection, int numThreads)
           
IteratorBasedMaster(java.util.Iterator<Problem<E>> problemIterator, int numThreads)
          Creates a new IteratorBasedMaster.
IteratorBasedMaster(java.util.Iterator<Problem<E>> problemIterator, int numThreads, java.lang.Integer maxStorageLength)
          Creates a new IteratorBasedMaster.
IteratorBasedMaster(Problem<E>[] array, int numThreads)
           
 
Method Summary
 boolean hasNext()
           
 java.util.Iterator<E> iterator()
           
 E next()
          If no computed solutions exist that have not already been returned, this method will block until the next problem finishes.
 E next(boolean remove)
          If no computed solutions exist that have not already been returned, this method will block until the next problem finishes.
 void remove()
          Does not do anything, as next() also removes elements from storage after returning them.
(package private)  void report(E solution, int id)
           
 void reset()
           
 void run()
          Runs the thread.
 
Methods inherited from class martin.common.compthreads.Master
getReportProgress, setReportProgress, startThread
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

problemIterator

private java.util.Iterator<Problem<E>> problemIterator

solutions

private java.util.Map<java.lang.Integer,E> solutions

solutionSemaphores

private java.util.Map<java.lang.Integer,java.util.concurrent.Semaphore> solutionSemaphores

threadsem

private java.util.concurrent.Semaphore threadsem

solutionsem

private java.util.concurrent.Semaphore solutionsem

addedsem

private java.util.concurrent.Semaphore addedsem

nextJobToRelease

private int nextJobToRelease

storageLimitSem

private java.util.concurrent.Semaphore storageLimitSem
Constructor Detail

IteratorBasedMaster

public IteratorBasedMaster(java.util.Iterator<Problem<E>> problemIterator,
                           int numThreads,
                           java.lang.Integer maxStorageLength)
Creates a new IteratorBasedMaster.

Parameters:
problemIterator - specifies an iterator which gives the problems that are to be computed.
numThreads - specifies the maximum number of problems that should be computed concurrently
maxStorageLength - specifies the maximum number of result objects that can be help in buffer memory

IteratorBasedMaster

public IteratorBasedMaster(java.util.Iterator<Problem<E>> problemIterator,
                           int numThreads)
Creates a new IteratorBasedMaster.

Parameters:
problemIterator - specifies an iterator which gives the problems that are to be computed.
numThreads - specifies the maximum number of problems that should be computed concurrently

IteratorBasedMaster

public IteratorBasedMaster(Problem<E>[] array,
                           int numThreads)

IteratorBasedMaster

public IteratorBasedMaster(java.util.Collection<Problem<E>> collection,
                           int numThreads)
Method Detail

run

public void run()
Runs the thread. This should not be called by the user - call startThread() instead.

Specified by:
run in interface java.lang.Runnable
Specified by:
run in class Master<E>

report

void report(E solution,
            int id)
Specified by:
report in class Master<E>

hasNext

public boolean hasNext()
Specified by:
hasNext in interface java.util.Iterator<E>
Returns:
whether all solutions (already computed or not) have already been returned by the next() function

next

public E next()
If no computed solutions exist that have not already been returned, this method will block until the next problem finishes. The problems are returned in the same order as they come in the problem iterator given to the constructor.

Specified by:
next in interface java.util.Iterator<E>
Returns:
the solution to the next problem in line to be returned.
Throws:
java.util.NoSuchElementException - if called when hasNext() == false

next

public E next(boolean remove)
If no computed solutions exist that have not already been returned, this method will block until the next problem finishes. The problems are returned in the same order as they come in the problem iterator given to the constructor.

Parameters:
remove - whether to also delete the returned object from the underlying storage data structures
Returns:
the solution to the next problem in line to be returned.
Throws:
java.util.NoSuchElementException - if called when hasNext() == false

reset

public void reset()

remove

public void remove()
Does not do anything, as next() also removes elements from storage after returning them.

Specified by:
remove in interface java.util.Iterator<E>

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>