SunSPOT API V6.0


com.sun.spot.service
Class Task

java.lang.Object
  extended by com.sun.spot.service.Task
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
Condition

public abstract class Task
extends Object
implements Runnable

Basic Task class to specify some code to be run starting at a specified time and then repeated periodically. The code will be run in its own Thread and in the context of the Isolate that created it.

Each time the task is run, its doTask() method is called. This method needs to be defined in your child Task class. For example to define a task to display the current temperature in an LED every 10 seconds, your code might look like this:

     // locate a thermometer
     final ITemperatureInput therm = (ITemperatureInput)Resources.lookup(ITemperatureInput.class);
     // get leftmost LED
     final ITriColorLED led = (ITriColorLED)Resources.lookup(ITriColorLED.class, "LED0");
     // define the task - check temperature every 10 seconds
     Task task = new Task(10 * 1000) {
         public void doTask() {
             double temp = therm.getCelsius();
             if (temp <= 5.0) {
                 led.setColor(LEDColor.BLUE);     // indicate cold
             } else if (temp <= 20.0) {
                 led.setColor(LEDColor.GREEN);    // indicate warm
             } else
                 led.setColor(LEDColor.RED);      // indicate hot
             }
         }
     }
     task.start();    // start the task running
  

Scheduling:

Each task will be run periodically. If a start time (hour:minute:second) is specified then the task will first be scheduled to be run at that time everyday. If an end time is specified the task will stop being run at that time. Note if the end time is earlier then the start time then the task will be run until the end time of the next day.

If the start hour is not specified (or set to -1), then the task will be run at min:sec in the current hour, or the next hour if the current time (h:m:s) has m > min. Likewise if both the start hour and minute are not specified then the task will be scheduled to start at :sec in the current or next minute. If the start hour is specified but the start minute or second is not, then the unspecified minute or second defaults to zero. The end time is handled in the same manner.

For example:

How to start/stop tasks

Calling task.start() will cause a Task to be scheduled with the TaskManager. Calling task.stop() will cause a Task to be unscheduled by the TaskManager.

Note: if executing the doTask() method throws an exception, only that call will be stopped; the task will still be rescheduled for future execution.

Author:
Ron Goldman

Field Summary
protected  boolean active
           
protected  int endHour
           
protected  int endMinute
           
protected  int endSecond
           
protected  long maxSleep
           
protected  long period
           
protected  boolean respectPhase
           
protected  long schedEndTime
           
protected  long schedTime
           
protected  boolean sleepOk
           
protected  int startHour
           
protected  int startMinute
           
protected  int startSecond
           
 
Constructor Summary
Task()
          Default constructor.
Task(long period)
          Basic Task constructor specifying how often to run task.
Task(long period, int startHour, int startMinute)
          Task constructor specifying how often to run task and what time of day (hh:mm) to start it running.
Task(long period, int startHour, int startMinute, int startSecond)
          Task constructor specifying how often to run task and what time of day (hh:mm:ss) to start it running.
Task(long period, int startHour, int startMinute, int endHour, int endMinute)
          Task constructor specifying how often to run task and what time of day to start and stop it running.
 
Method Summary
protected  void computeScheduledTime()
           
 void deferToDeepSleep(boolean defer, long maxSleepTime)
          Specify if this task should defer to deep sleep and if so for how long.
abstract  void doTask()
          Method called when Task is run.
 int getEndHour()
          Get what hour of day to stop running this task.
 int getEndMinute()
          Get what minute of the hour to stop running this task.
 int getEndSecond()
          Get what second of the minute to stop running this task.
 long getPeriod()
          Get the interval to reschedule the task.
 long getScheduledTime()
          Get the time the task is next scheduled to be run.
 int getStartHour()
          Get what hour of day to start running this task.
 int getStartMinute()
          Get what minute of the hour to start running this task.
 int getStartSecond()
          Get what second of the minute to start running this task.
protected  void init()
           
 boolean isActive()
          Check if the task is currently scheduled for execution.
protected  void reschedule()
           
 void run()
          Internal method.
 void setEndHour(int endHour)
          Set what hour of day to stop running this task.
 void setEndMinute(int endMinute)
          Set what minute of the hour to stop running this task.
 void setEndSecond(int endSecond)
          Set what second of the minute to stop running this task.
 void setEndTime(int endHour, int endMinute)
          Set what time of day (hh:mm) to stop running this task.
 void setEndTime(int endHour, int endMinute, int endSecond)
          Set what time of day (hh:mm:ss) to stop running this task.
 void setPeriod(long t)
          Set the interval to reschedule the task.
 void setScheduledTime(long t)
          Set the time when the task is next scheduled to be run.
 void setStartHour(int startHour)
          Set what hour of day to start running this task.
 void setStartMinute(int startMinute)
          Set what minute of the hour to start running this task.
 void setStartSecond(int startSecond)
          Set what second of the minute to start running this task.
 void setStartTime(int startHour, int startMinute)
          Set what time of day (hh:mm) to start running this task.
 void setStartTime(int startHour, int startMinute, int startSecond)
          Set what time of day (hh:mm:ss) to start running this task.
 void start()
          Schedule the task for subsequent execution.
 void stop()
          Remove the task from subsequent execution.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

period

protected long period

startHour

protected int startHour

startMinute

protected int startMinute

startSecond

protected int startSecond

endHour

protected int endHour

endMinute

protected int endMinute

endSecond

protected int endSecond

schedTime

protected long schedTime

schedEndTime

protected long schedEndTime

active

protected boolean active

sleepOk

protected boolean sleepOk

respectPhase

protected boolean respectPhase

maxSleep

protected long maxSleep
Constructor Detail

Task

public Task()
Default constructor.


Task

public Task(long period)
Basic Task constructor specifying how often to run task.

Parameters:
period - run task interval in milliseconds

Task

public Task(long period,
            int startHour,
            int startMinute)
Task constructor specifying how often to run task and what time of day (hh:mm) to start it running.

Parameters:
period - time interval in milliseconds to wait before rescheduling this task
startHour - hour of the day (0-23) to start running task (-1 = unspecified)
startMinute - minute of the hour (0-59) to start running task (-1 = unspecified)

Task

public Task(long period,
            int startHour,
            int startMinute,
            int startSecond)
Task constructor specifying how often to run task and what time of day (hh:mm:ss) to start it running.

Parameters:
period - time interval in milliseconds to wait before rescheduling this task
startHour - hour of the day (0-23) to start running task (-1 = unspecified)
startMinute - minute of the hour (0-59) to start running task (-1 = unspecified)
startSecond - second of the minute (0-59) to start running task (-1 = unspecified)

Task

public Task(long period,
            int startHour,
            int startMinute,
            int endHour,
            int endMinute)
Task constructor specifying how often to run task and what time of day to start and stop it running.

Parameters:
period - time interval in milliseconds to wait before rescheduling this task
startHour - hour of the day (0-23) to start running task (-1 = unspecified)
startMinute - minute of the hour (0-59) to start running task (-1 = unspecified)
endHour - hour of the day (0-23) to stop running task (-1 = unspecified)
endMinute - minute of the hour (0-59) to stop running task (-1 = unspecified)
Method Detail

doTask

public abstract void doTask()
                     throws Exception
Method called when Task is run. User must define it.

Throws:
Exception

init

protected void init()

setStartTime

public void setStartTime(int startHour,
                         int startMinute)
Set what time of day (hh:mm) to start running this task.

Parameters:
startHour - hour of the day (0-23) to start running task
startMinute - minute of the hour (0-59) to start running task

setStartTime

public void setStartTime(int startHour,
                         int startMinute,
                         int startSecond)
Set what time of day (hh:mm:ss) to start running this task.

Parameters:
startHour - hour of the day (0-23) to start running task
startMinute - minute of the hour (0-59) to start running task
startSecond - second of the minute (0-59) to start running task

setEndTime

public void setEndTime(int endHour,
                       int endMinute)
Set what time of day (hh:mm) to stop running this task.

Parameters:
endHour - hour of the day (0-23) to stop running task
endMinute - minute of the hour (0-59) to stop running task

setEndTime

public void setEndTime(int endHour,
                       int endMinute,
                       int endSecond)
Set what time of day (hh:mm:ss) to stop running this task.

Parameters:
endHour - hour of the day (0-23) to stop running task
endMinute - minute of the hour (0-59) to stop running task
endSecond - second of the minute (0-59) to stop running task

setStartHour

public void setStartHour(int startHour)
Set what hour of day to start running this task.

Parameters:
startHour - hour of the day (0-23) to start running task

setStartMinute

public void setStartMinute(int startMinute)
Set what minute of the hour to start running this task.

Parameters:
startMinute - minute of the hour (0-59) to start running task

setStartSecond

public void setStartSecond(int startSecond)
Set what second of the minute to start running this task.

Parameters:
startSecond - second of the minute (0-59) to start running task

setEndHour

public void setEndHour(int endHour)
Set what hour of day to stop running this task.

Parameters:
endHour - hour of the day (0-23) to stop running task

setEndMinute

public void setEndMinute(int endMinute)
Set what minute of the hour to stop running this task.

Parameters:
endMinute - minute of the hour (0-59) to stop running task

setEndSecond

public void setEndSecond(int endSecond)
Set what second of the minute to stop running this task.

Parameters:
endSecond - second of the minute (0-59) to stop running task

getScheduledTime

public long getScheduledTime()
Get the time the task is next scheduled to be run.

Returns:
time in milliseconds when task is to be run

setScheduledTime

public void setScheduledTime(long t)
Set the time when the task is next scheduled to be run.

Parameters:
t - time in milliseconds when task is to be run

getPeriod

public long getPeriod()
Get the interval to reschedule the task.

Returns:
the interval to reschedule the task in milliseconds

setPeriod

public void setPeriod(long t)
Set the interval to reschedule the task.

Parameters:
t - the interval to reschedule the task in milliseconds

getStartHour

public int getStartHour()
Get what hour of day to start running this task.

Returns:
hour of the day (0-23) to start running task

getStartMinute

public int getStartMinute()
Get what minute of the hour to start running this task.

Returns:
minute of the hour (0-59) to start running task

getStartSecond

public int getStartSecond()
Get what second of the minute to start running this task.

Returns:
second of the minute (0-59) to start running task

getEndHour

public int getEndHour()
Get what hour of day to stop running this task.

Returns:
hour of the day (0-23) to stop running task

getEndMinute

public int getEndMinute()
Get what minute of the hour to stop running this task.

Returns:
minute of the hour (0-59) to stop running task

getEndSecond

public int getEndSecond()
Get what second of the minute to stop running this task.

Returns:
second of the minute (0-59) to stop running task

deferToDeepSleep

public void deferToDeepSleep(boolean defer,
                             long maxSleepTime)
Specify if this task should defer to deep sleep and if so for how long. Currently this has no effect.

Parameters:
defer - true if task will defer
maxSleepTime - maximum time before task will be run (<=0 means no limit)

start

public void start()
Schedule the task for subsequent execution. Ignored if the task has already been started.


stop

public void stop()
Remove the task from subsequent execution. Ignored if the task has already been stopped.


isActive

public boolean isActive()
Check if the task is currently scheduled for execution.

Returns:
true if the task is currently active, false if it is stopped

computeScheduledTime

protected void computeScheduledTime()

reschedule

protected void reschedule()

run

public void run()
Internal method. Do not call explicitly.

Specified by:
run in interface Runnable
See Also:
Thread.run()

SunSPOT API V6.0


Copyright © 2006-2010 Oracle. All Rights Reserved.