public abstract class FJTask extends Object implements Runnable
Wrap(Runnable r)
can be used to
wrap an existing Runnable object in a FJTask.
FJTaskRunnerGroup.execute(FJTask) can be used to
initiate a FJTask from a non-FJTask thread.
And FJTaskRunnerGroup.invoke(FJTask) can be used to initiate
a FJTask and then wait for it to complete before returning.
These are the only entry-points from normal threads to FJTasks.
Most FJTask methods themselves may only be called from within running FJTasks.
They throw ClassCastExceptions if they are not,
reflecting the fact that these methods
can only be executed using FJTaskRunner threads, not generic
java.lang.Threads.
There are three different ways to run a FJTask,
with different scheduling semantics:
FJTaskRunner,
FJTaskRunnerGroup| 限定符和类型 | 类和说明 |
|---|---|
static class |
FJTask.Par
A
new Par, when executed,
runs the tasks provided in the constructor in parallel using
coInvoke(tasks). |
static class |
FJTask.Par2
A
new Par(task1, task2), when executed,
runs task1 and task2 in parallel using coInvoke(task1, task2). |
static class |
FJTask.Seq
A
new Seq, when executed,
invokes each task provided in the constructor, in order. |
static class |
FJTask.Seq2
A
new Seq2(task1, task2), when executed,
invokes task1 and then task2, in order. |
static class |
FJTask.Wrap
A FJTask that holds a Runnable r, and calls r.run when executed.
|
| 构造器和说明 |
|---|
FJTask() |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
cancel()
Set the termination status of this task.
|
static void |
coInvoke(FJTask[] tasks)
Fork all tasks in array, and await their completion.
|
static void |
coInvoke(FJTask task1,
FJTask task2)
Fork both tasks and then wait for their completion.
|
void |
fork()
Arrange for execution of a strictly dependent task.
|
static FJTaskRunner |
getFJTaskRunner()
Return the FJTaskRunner thread running the current FJTask.
|
static FJTaskRunnerGroup |
getFJTaskRunnerGroup()
Return the FJTaskRunnerGroup of the thread running the current FJTask.
|
static void |
invoke(FJTask t)
Immediately execute task t by calling its run method.
|
boolean |
isDone()
Return true if current task has terminated or been cancelled.
|
void |
join()
Yield until this task isDone.
|
static FJTask |
par(FJTask[] tasks)
Construct and return a FJTask object that, when executed, will
invoke the tasks in the tasks array in parallel using coInvoke
|
static FJTask |
par(FJTask task1,
FJTask task2)
Construct and return a FJTask object that, when executed, will
invoke task1 and task2, in parallel
|
void |
reset()
Clear the termination status of this task.
|
static FJTask |
seq(FJTask[] tasks)
Construct and return a FJTask object that, when executed, will
invoke the tasks in the tasks array in array order
|
static FJTask |
seq(FJTask task1,
FJTask task2)
Construct and return a FJTask object that, when executed, will
invoke task1 and task2, in order
|
protected void |
setDone()
Indicate termination.
|
void |
start()
Execute this task.
|
static void |
yield()
Allow the current underlying FJTaskRunner thread to process other tasks.
|
public static FJTaskRunner getFJTaskRunner()
ClassCastException - if caller thread is not a
running FJTask.public static FJTaskRunnerGroup getFJTaskRunnerGroup()
ClassCastException - if caller thread is not a
running FJTask.public final boolean isDone()
protected final void setDone()
public void cancel()
public void reset()
public void start()
ClassCastException - if caller thread is not
running in a FJTaskRunner thread.public void fork()
ClassCastException - if caller thread is not
running in a FJTaskRunner thread.public static void yield()
ClassCastException - if caller thread is not
running in a FJTaskRunner thread.public void join()
while(!isDone()) yield(); ClassCastException - if caller thread is not
running in a FJTaskRunner thread.public static void invoke(FJTask t)
public static void coInvoke(FJTask task1, FJTask task2)
task1.fork(); task2.fork(); task2.join(); task1.join();As a simple classic example, here is a class that computes the Fibonacci function:
public class Fib extends FJTask {
// Computes fibonacci(n) = fibonacci(n-1) + fibonacci(n-2); for n > 1
// fibonacci(0) = 0;
// fibonacci(1) = 1.
// Value to compute fibonacci function for.
// It is replaced with the answer when computed.
private volatile int number;
public Fib(int n) { number = n; }
public int getAnswer() {
if (!isDone()) throw new Error("Not yet computed");
return number;
}
public void run() {
int n = number;
if (n > 1) {
Fib f1 = new Fib(n - 1);
Fib f2 = new Fib(n - 2);
coInvoke(f1, f2); // run these in parallel
// we know f1 and f2 are computed, so just directly access numbers
number = f1.number + f2.number;
}
}
public static void main(String[] args) { // sample driver
try {
int groupSize = 2; // 2 worker threads
int num = 35; // compute fib(35)
FJTaskRunnerGroup group = new FJTaskRunnerGroup(groupSize);
Fib f = new Fib(num);
group.invoke(f);
int result = f.getAnswer();
System.out.println(" Answer: " + result);
}
catch (InterruptedException ex) {
System.out.println("Interrupted");
}
}
}
ClassCastException - if caller thread is not
running in a FJTaskRunner thread.public static void coInvoke(FJTask[] tasks)
for (int i = 0; i < tasks.length; ++i) tasks[i].fork(); for (int i = 0; i < tasks.length; ++i) tasks[i].join();
public static FJTask seq(FJTask[] tasks)
public static FJTask par(FJTask[] tasks)
public static FJTask seq(FJTask task1, FJTask task2)
Copyright © 2024. All rights reserved.