|
SunSPOT API V6.0 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.spot.service.Task
public abstract class Task
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.
| 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 |
|---|
protected long period
protected int startHour
protected int startMinute
protected int startSecond
protected int endHour
protected int endMinute
protected int endSecond
protected long schedTime
protected long schedEndTime
protected boolean active
protected boolean sleepOk
protected boolean respectPhase
protected long maxSleep
| Constructor Detail |
|---|
public Task()
public Task(long period)
period - run task interval in milliseconds
public Task(long period,
int startHour,
int startMinute)
period - time interval in milliseconds to wait before rescheduling this taskstartHour - 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)
public Task(long period,
int startHour,
int startMinute,
int startSecond)
period - time interval in milliseconds to wait before rescheduling this taskstartHour - 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)
public Task(long period,
int startHour,
int startMinute,
int endHour,
int endMinute)
period - time interval in milliseconds to wait before rescheduling this taskstartHour - 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 |
|---|
public abstract void doTask()
throws Exception
Exceptionprotected void init()
public void setStartTime(int startHour,
int startMinute)
startHour - hour of the day (0-23) to start running taskstartMinute - minute of the hour (0-59) to start running task
public void setStartTime(int startHour,
int startMinute,
int startSecond)
startHour - hour of the day (0-23) to start running taskstartMinute - minute of the hour (0-59) to start running taskstartSecond - second of the minute (0-59) to start running task
public void setEndTime(int endHour,
int endMinute)
endHour - hour of the day (0-23) to stop running taskendMinute - minute of the hour (0-59) to stop running task
public void setEndTime(int endHour,
int endMinute,
int endSecond)
endHour - hour of the day (0-23) to stop running taskendMinute - minute of the hour (0-59) to stop running taskendSecond - second of the minute (0-59) to stop running taskpublic void setStartHour(int startHour)
startHour - hour of the day (0-23) to start running taskpublic void setStartMinute(int startMinute)
startMinute - minute of the hour (0-59) to start running taskpublic void setStartSecond(int startSecond)
startSecond - second of the minute (0-59) to start running taskpublic void setEndHour(int endHour)
endHour - hour of the day (0-23) to stop running taskpublic void setEndMinute(int endMinute)
endMinute - minute of the hour (0-59) to stop running taskpublic void setEndSecond(int endSecond)
endSecond - second of the minute (0-59) to stop running taskpublic long getScheduledTime()
public void setScheduledTime(long t)
t - time in milliseconds when task is to be runpublic long getPeriod()
public void setPeriod(long t)
t - the interval to reschedule the task in millisecondspublic int getStartHour()
public int getStartMinute()
public int getStartSecond()
public int getEndHour()
public int getEndMinute()
public int getEndSecond()
public void deferToDeepSleep(boolean defer,
long maxSleepTime)
defer - true if task will defermaxSleepTime - maximum time before task will be run (<=0 means no limit)public void start()
public void stop()
public boolean isActive()
protected void computeScheduledTime()
protected void reschedule()
public void run()
run in interface RunnableThread.run()
|
SunSPOT API V6.0 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||