com.sun.jdmk.tasks
Class DaemonTaskServer

java.lang.Object
  extended by com.sun.jdmk.tasks.DaemonTaskServer
All Implemented Interfaces:
TaskServer

public class DaemonTaskServer
extends Object
implements TaskServer

This class implements a Task Server that runs in its own thread. Objects can submit tasks to this object, and they will be executed in background in this object daemon thread. The submitted tasks should not block or last too long - because it will prevent following tasks to be executed. When a task producer calls the submitTask(Task) method, the task is put inside a FIFO list, and the Task Server Thread is waken up, if necessary. After that, the method return.

The Task Server Thread then asynchronously takes the tasks out of the FIFO list, and invokes the run() method.

This mechanism guarantee that the Task producer will spend a minimum time invoking the tasks, and will not be blocked.

Since:
Java DMK 5.0
See Also:
Task, TaskServer

Constructor Summary
DaemonTaskServer()
           
 
Method Summary
protected  void cancel(Task task)
          Cancel the given task.
protected  void execute(Task task)
          Execute the given task.
 void reset()
          Reset the DaemonTaskServer.
 void start()
          Start the Task Server Thread.
 void start(int threadPriority)
          Start the Task Server Thread.
 void stop()
          Stop the Task Server Thread.
 void submitTask(Task task)
          Submit a task.
 void terminate()
          Stop the Task Server Thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DaemonTaskServer

public DaemonTaskServer()
Method Detail

execute

protected void execute(Task task)
                throws InterruptedException
Execute the given task. This method simply calls task.run(). It is provided as a customization hook for subclasses.

Parameters:
task - The task to execute.
Throws:
InterruptedException - if the task is interrupted.

cancel

protected void cancel(Task task)
               throws InterruptedException
Cancel the given task. This method simply calls task.cancel(). It is provided as a customization hook for subclasses.

Parameters:
task - The task to cancel.
Throws:
InterruptedException - if cancel is interrupted.

submitTask

public void submitTask(Task task)
Submit a task. The submitted task is put inside a FIFO list, and the Task Server Thread is waken up, if necessary. After that, the method returns. The task will be executed asynchronously by the Task Server Thread.

Specified by:
submitTask in interface TaskServer
Parameters:
task - The Task to execute.

stop

public void stop()
          throws InterruptedException
Stop the Task Server Thread. Wait for the thread to die.

Throws:
InterruptedException - if stop is interrupted.

terminate

public void terminate()
               throws InterruptedException
Stop the Task Server Thread. Wait for the thread to die. Then flush the Task FIFO, canceling any remaining task. During this time, the object will reject any call to submitTask(). When terminated, the object can not be started again, unless reset() is called.

Throws:
InterruptedException - if cancel is interrupted.

reset

public void reset()
Reset the DaemonTaskServer. This method makes it possible to reuse the DaemonTaskServer after a terminate()

Throws:
IllegalStateException - if the DaemonTaskServer is running.

start

public void start()
Start the Task Server Thread.

Throws:
IllegalStateException - if the DaemonTaskServer is terminated.

start

public void start(int threadPriority)
Start the Task Server Thread. Assign the specified priority to the internal daemon thread.

Parameters:
threadPriority - Priority of the daemon thread see Thread.
Throws:
IllegalStateException - if the DaemonTaskServer is terminated.

Open Source build 02
opendmk-1.0-b02 2007.10.01_19:17:46_MEST

Copyright 1998-2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.