Logo Search packages:      
Sourcecode: scala version File versions  Download package

scala::actors::FJTaskRunnerGroup Class Reference

Inheritance diagram for scala::actors::FJTaskRunnerGroup:

scala::actors::IFJTaskRunnerGroup

List of all members.


Detailed Description

A stripped down analog of a ThreadGroup used for establishing and managing FJTaskRunner threads. ThreadRunnerGroups serve as the control boundary separating the general world of normal threads from the specialized world of FJTasks.

By intent, this class does not subclass java.lang.ThreadGroup, and does not support most methods found in ThreadGroups, since they would make no sense for FJTaskRunner threads. In fact, the class does not deal with ThreadGroups at all. If you want to restrict a FJTaskRunnerGroup to a particular ThreadGroup, you can create it from within that ThreadGroup.

The main contextual parameter for a FJTaskRunnerGroup is the group size, established in the constructor. Groups must be of a fixed size. There is no way to dynamically increase or decrease the number of threads in an existing group.

In general, the group size should be equal to the number of CPUs on the system. (Unfortunately, there is no portable means of automatically detecting the number of CPUs on a JVM, so there is no good way to automate defaults.) In principle, when FJTasks are used for computation-intensive tasks, having only as many threads as CPUs should minimize bookkeeping overhead and contention, and so maximize throughput. However, because FJTaskRunners lie atop Java threads, and in turn operating system thread support and scheduling policies, it is very possible that using more threads than CPUs will improve overall throughput even though it adds to overhead. This will always be so if FJTasks are I/O bound. So it may pay to experiment a bit when tuning on particular platforms. You can also use setRunPriorities to either increase or decrease the priorities of active threads, which may interact with group size choice.

In any case, overestimating group sizes never seriously degrades performance (at least within reasonable bounds). You can also use a value less than the number of CPUs in order to reserve processing for unrelated threads.

There are two general styles for using a FJTaskRunnerGroup. You can create one group per entire program execution, for example as a static singleton, and use it for all parallel tasks:

 class Tasks {
   static FJTaskRunnerGroup group;
   public void initialize(int groupsize) {
      group = new FJTaskRunnerGroup(groupSize);
   }
   // ...
 }
 
Alternatively, you can make new groups on the fly and use them only for particular task sets. This is more flexible,, and leads to more controllable and deterministic execution patterns, but it encounters greater overhead on startup. Also, to reclaim system resources, you should call FJTaskRunnerGroup.interruptAll when you are done using one-shot groups. Otherwise, because FJTaskRunners set Thread.isDaemon status, they will not normally be reclaimed until program termination.

The main supported methods are execute, which starts a task processed by FJTaskRunner threads, and invoke, which starts one and waits for completion. For example, you might extend the above FJTasks class to support a task-based computation, say, the Fib class from the FJTask documentation:

 class Tasks { // continued
   // ...
   static int fib(int n) {
     try {
       Fib f = new Fib(n);
       group.invoke(f);
       return f.getAnswer();
     }
     catch (InterruptedException ex) {
       throw new Error("Interrupted during computation");
     }
   }
 }
 

Method stats() can be used to monitor performance. Both FJTaskRunnerGroup and FJTaskRunner may be compiled with the compile-time constant COLLECT_STATS set to false. In this case, various simple counts reported in stats() are not collected. On platforms tested, this leads to such a tiny performance improvement that there is very little motivation to bother.

[Introduction to this package. ]

See also:
FJTask

FJTaskRunner

Definition at line 122 of file FJTaskRunnerGroup.java.


Public Member Functions

synchronized void checkActive (FJTaskRunner t, long scans)
boolean checkPoolSize ()
void execute (Runnable r) throws InterruptedException
void executeTask (FJTask t)
boolean existsTask ()
 FJTaskRunnerGroup (int groupSize)
synchronized int getActiveCount ()
FJTaskRunner[] getArray ()
LinkedQueue getEntryQueue ()
void interruptAll ()
void invoke (Runnable r) throws InterruptedException
FJTask pollEntryQueue ()
synchronized void setActive (FJTaskRunner t)
synchronized void setInactive (FJTaskRunner t)
synchronized void setRunPriorities (int pri)
synchronized void setScanPriorities (int pri)
int size ()
void stats ()

Protected Member Functions

synchronized boolean getActive (FJTaskRunner t)
void initializeThreads ()
synchronized void signalNewTask ()

Protected Attributes

int activeCount = 0
int nstarted = 0
FJTaskRunner[] threads

Package Functions

void snapshot () throws InterruptedException

Package Attributes

int entries = 0
final LinkedQueue entryQueue = new LinkedQueue()
long initTime = 0

Static Package Attributes

static final boolean COLLECT_STATS = true
static final int DEFAULT_SCAN_PRIORITY = Thread.MIN_PRIORITY+1
static final long MAX_SLEEP_TIME = 100
static final long SCANS_PER_SLEEP = 15

Classes

class  InvokableFJTask

The documentation for this class was generated from the following file:

Generated by  Doxygen 1.6.0   Back to index