edu.ull.cgunay.utils
Class ParallelTask

java.lang.Object
  |
  +--edu.ull.cgunay.utils.ParallelTask
All Implemented Interfaces:
Serializable, Simulation

public class ParallelTask
extends Object
implements Simulation

Parallel analog of an iteration where a given task is executed on each of the items of a collection concurrently. The init() method creates threads for all items and the step() method contains the synchronization algorithm required to start and end all tasks concurrently. stop() terminates all threads and therefore relinquishes resources.

Subsequent calls to step() is mutually exclusive by ensuring that the the new step does not start until the previous step is completed.

TODO: Make it more expressive.

Version:
$Revision: 1.6 $ for this file
Author:
Cengiz Gunay
See Also:
Iteration, Serialized Form

Field Summary
 Collection collection
          The collection on which to iterate.
 int runcount
          Semaphore for threads that are running.
(package private)  Task task
          Task to be executed at each step by each thread.
(package private)  ThreadGroup threads
          Group together the threads in order to terminate them at once.
 int waitcount
          Semaphore for threads that are waiting.
 
Constructor Summary
ParallelTask(Collection collection, Task task)
          Set collection and task.
 
Method Summary
 void init()
          Start a new thread containing the Objective runnable for each element in the collection.
 void reset()
          Resets value of runcount to max # of threads.
 void step()
          Does the following: Wait for all threads to go in wait() (until waitcount = max) Notify all of them, therefore make them execute the task.
 void stop()
          Terminates all threads, presumably releasing all resources.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

collection

public Collection collection
The collection on which to iterate.


waitcount

public volatile int waitcount
Semaphore for threads that are waiting.


runcount

public volatile int runcount
Semaphore for threads that are running.


task

Task task
Task to be executed at each step by each thread.


threads

ThreadGroup threads
Group together the threads in order to terminate them at once.

Constructor Detail

ParallelTask

public ParallelTask(Collection collection,
                    Task task)
Set collection and task.

Parameters:
collection - a Collection value
task - a Task value
Method Detail

reset

public void reset()
Resets value of runcount to max # of threads.


init

public void init()
Start a new thread containing the Objective runnable for each element in the collection.

Specified by:
init in interface Simulation
See Also:
Objective

step

public void step()
Does the following:
  1. Wait for all threads to go in wait() (until waitcount = max)
  2. Notify all of them, therefore make them execute the task.
  3. Wait for all of the to finish (until runcount == 0).

    Specified by:
    step in interface Simulation

stop

public void stop()
Terminates all threads, presumably releasing all resources.

Specified by:
stop in interface Simulation