Quick Start

Download

Step 1: download the latest jar from the downloads page on SourceForge.net.
Step 2: add it to your classpath.

Examples

This simple example of a looping thread is all that you need to implement to support cancellation, pausing and many other features out of the box. It can be run in a child thread or in the same thread as the caller.

public class MyTask implements Callable<String> {
  @Context
  private ExecutionContext execution;

  public String call() throws Exception {
    String result = null;
    while (!execution.isCancellationRequested(true)) {
      // do stuff
    }
    return result;
  }
}

The following code snippet will call the above task in the current thread, wait for it to complete, and get the result. Any exceptions thrown by the task will be thrown by the call to TaskExecutor.execute(task):

Callable<String> task = new MyTask();
String result = TaskExecutor.execute(task);

The following code snippet will start the task running in a separate thread, do something else while the task runs, and finally get the tasks's result. The call to future.get() will block until the task completes or throws an exception:

Callable<String> task = new MyTask();
TaskFuture<String> future = TaskExecutor.start(task);
// do other stuff while tasks executes
String result = future.get();

This example illustrates how you can create code that doesn't know in advance whether the task was executed synchronously (in the same thread), or asynchronously (in a separate thread):

boolean asynch = ...;
Callable<String> task = new MyTask();
TaskFuture<String> future = TaskExecutor.prepare(asynch, task).start();
doStuffWithResult(future);
(The call to TaskExecutor.prepare() has many other uses, check out the Javadocs for more details.)

Other Examples

There are lots of other examples. Check out the 'examples' jar in the downloads page on SourceForge.net.