org.openide.util
Class Task

java.lang.Object
  extended by org.openide.util.Task
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
RequestProcessor.Task

public class Task
extends java.lang.Object
implements java.lang.Runnable

A task that may be executed in a separate thread and permits examination of its status. Other threads can check if it is finished or wait for it to finish.

For example:

 Runnable r = new Runnable () {
   public void run () {
     // do something
   }
 };
 Task task = new Task (r);
 RequestProcessor.postRequest (task);
 

In a different thread one can then test task.isFinished () or wait for it with task.waitFinished ().

Author:
Jaroslav Tulach

Field Summary
static Task EMPTY
          Dummy task which is already finished.
(package private)  java.lang.Runnable run
          what to run
 
Constructor Summary
protected Task()
          Constructor for subclasses that wants to control whole execution itself.
  Task(java.lang.Runnable run)
          Create a new task.
 
Method Summary
 void addTaskListener(TaskListener l)
          Add a listener to the task.
(package private)  java.lang.String debug()
          Reveal the identity of the worker runnable.
 boolean isFinished()
          Test whether the task has finished running.
protected  void notifyFinished()
          Notify all waiters that this task has finished.
protected  void notifyRunning()
          Changes the state of the task to be running.
 void removeTaskListener(TaskListener l)
          Remove a listener from the task.
 void run()
          Start the task.
 java.lang.String toString()
           
 void waitFinished()
          Wait until the task is finished.
 boolean waitFinished(long milliseconds)
          Wait until the task is finished, but only a given time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY

public static final Task EMPTY
Dummy task which is already finished.


run

final java.lang.Runnable run
what to run

Constructor Detail

Task

public Task(java.lang.Runnable run)
Create a new task. The runnable should provide its own error-handling, as by default thrown exceptions are simply logged and not rethrown.

Parameters:
run - runnable to run that computes the task

Task

protected Task()
Constructor for subclasses that wants to control whole execution itself.

Since:
1.5
Method Detail

isFinished

public final boolean isFinished()
Test whether the task has finished running.

Returns:
true if so

waitFinished

public void waitFinished()
Wait until the task is finished. Changed not to be final in version 1.5


waitFinished

public boolean waitFinished(long milliseconds)
                     throws java.lang.InterruptedException
Wait until the task is finished, but only a given time.

Parameters:
milliseconds - time in milliseconds to wait for the result
Returns:
true if the task is really finished, or false if the time out has been exceeded
Throws:
java.lang.InterruptedException - when the waiting has been interrupted
Since:
5.0

notifyRunning

protected final void notifyRunning()
Changes the state of the task to be running. Any call after this one and before notifyFinished to waitFinished blocks.

Since:
1.5

notifyFinished

protected final void notifyFinished()
Notify all waiters that this task has finished.

See Also:
run

run

public void run()
Start the task. When it finishes (even with an exception) it calls notifyFinished(). Subclasses may override this method, but they then need to call notifyFinished() explicitly.

Note that this call runs synchronously, but typically the creator of the task will call this method in a separate thread.

Specified by:
run in interface java.lang.Runnable

addTaskListener

public void addTaskListener(TaskListener l)
Add a listener to the task.

Parameters:
l - the listener to add

removeTaskListener

public void removeTaskListener(TaskListener l)
Remove a listener from the task.

Parameters:
l - the listener to remove

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

debug

java.lang.String debug()
Reveal the identity of the worker runnable. Used for debugging from RequestProcessor.