public class FJTaskRunnerGroup extends Object implements Executor
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. ]FJTask,
FJTaskRunner| 限定符和类型 | 类和说明 |
|---|---|
protected static class |
FJTaskRunnerGroup.InvokableFJTask
Wrap wait/notify mechanics around a task so that
invoke() can wait it out
|
| 限定符和类型 | 字段和说明 |
|---|---|
protected int |
activeCount
Number of threads that are not waiting for work
|
protected LinkedQueue |
entryQueue
Group-wide queue for tasks entered via execute()
|
protected int |
nstarted
Number of threads that have been started.
|
protected FJTaskRunner[] |
threads
The threads in this group
|
| 构造器和说明 |
|---|
FJTaskRunnerGroup(int groupSize)
Create a FJTaskRunnerGroup with the indicated number
of FJTaskRunner threads.
|
| 限定符和类型 | 方法和说明 |
|---|---|
protected void |
checkActive(FJTaskRunner t,
long scans)
Set active status of thread t to false, and
then wait until: (a) there is a task in the entry
queue, or (b) other threads are active, or (c) the current
thread is interrupted.
|
void |
execute(Runnable r)
Arrange for execution of the given task
by placing it in a work queue.
|
void |
executeTask(FJTask t)
Specialized form of execute called only from within FJTasks
|
protected boolean |
getActive(FJTaskRunner t)
Return active status of t.
|
int |
getActiveCount()
Return the number of threads that are not idly waiting for work.
|
protected FJTaskRunner[] |
getArray()
Return the array of threads in this group.
|
protected void |
initializeThreads()
Create all FJTaskRunner threads in this group.
|
void |
interruptAll()
Try to shut down all FJTaskRunner threads in this group
by interrupting them all.
|
void |
invoke(Runnable r)
Start a task and wait it out.
|
protected FJTask |
pollEntryQueue()
Return a task from entry queue, or null if empty.
|
protected void |
setActive(FJTaskRunner t)
Set active status of thread t to true, and notify others
that might be waiting for work.
|
protected void |
setInactive(FJTaskRunner t)
Set active status of thread t to false.
|
void |
setRunPriorities(int pri)
Set the priority to use while a FJTaskRunner is
actively running tasks.
|
void |
setScanPriorities(int pri)
Set the priority to use while a FJTaskRunner is
polling for new tasks to perform.
|
protected void |
signalNewTask()
Start or wake up any threads waiting for work
|
int |
size()
Return the number of FJTaskRunner threads in this group
|
void |
stats()
Prints various snapshot statistics to System.out.
|
protected final FJTaskRunner[] threads
protected final LinkedQueue entryQueue
protected int activeCount
protected int nstarted
public FJTaskRunnerGroup(int groupSize)
public void execute(Runnable r) throws InterruptedException
FJTask.Wrap.execute 在接口中 ExecutorInterruptedException - if current Thread is
currently interruptedpublic void executeTask(FJTask t)
public void invoke(Runnable r) throws InterruptedException
InterruptedException - if current Thread is
interrupted before completion of the task.public void interruptAll()
public void setScanPriorities(int pri)
public void setRunPriorities(int pri)
public int size()
public int getActiveCount()
public void stats()
protected FJTaskRunner[] getArray()
protected FJTask pollEntryQueue()
protected boolean getActive(FJTaskRunner t)
protected void setActive(FJTaskRunner t)
protected void setInactive(FJTaskRunner t)
protected void checkActive(FJTaskRunner t, long scans)
protected void signalNewTask()
protected void initializeThreads()
Copyright © 2024. All rights reserved.