Package trclib.robotcore
Class TrcPeriodicThread<T>
java.lang.Object
trclib.robotcore.TrcPeriodicThread<T>
- Type Parameters:
T
- specifies the data type that the periodic task will be acquiring/processing.
This class implements a platform independent periodic task by using a separate thread. When enabled, the thread
periodically calls the runPeriodic method. Generally, this class is used by TrcTaskMgr to create a standalone
thread that runs the periodic task when STANDALONE_TASK is registered. By doing so, the standalone task doesn't
slow down the main robot thread even if it is performing something that may block for long period of time.
In rare occasions, this class can also be used by a sensor that requires long period of time to acquire its
data but arguably, one could just call TrcTaskMgr to create a STANDALONE_TASK instead. In other words, this
class is mainly used by TrcTaskMgr, there is really no reason for others to use this class. One should always
use TrcTaskMgr to create a STANDALONE_TASK.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
private class
This class keeps track of the state of the periodic task. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Object
private final String
private static final AtomicInteger
private long
private static boolean
private final TrcPeriodicThread.PeriodicTask
private final TrcPeriodicThread<T>.TaskState
final TrcDbgTrace
-
Constructor Summary
ConstructorsConstructorDescriptionTrcPeriodicThread
(String instanceName, TrcPeriodicThread.PeriodicTask task, Object context) Constructor: Create an instance of the object.TrcPeriodicThread
(String instanceName, TrcPeriodicThread.PeriodicTask task, Object context, int taskPriority) Constructor: Create an instance of the object. -
Method Summary
Modifier and TypeMethodDescriptiongetData()
This method returns the acquired/processed data.static int
This method returns the number of active threads.long
This method returns the periodic task processing interval.boolean
This method returns the state of the periodic task.boolean
This method checks if the periodic task has been terminated.void
run()
This method runs the periodic processing task.void
This method is called to set new data after new data have been acquired/processed.void
setProcessingInterval
(long interval) This method sets the periodic task processing interval.static void
setRobotInitialized
(boolean init) This method is called by the framework scheduler to inform us whether robotInit has finished.void
setTaskEnabled
(boolean enabled) This method enables/disables the periodic task.void
This method is called to terminate the periodic task.toString()
This method returns the instance name.
-
Field Details
-
robotInitialized
private static boolean robotInitialized -
numActiveThreads
-
tracer
-
instanceName
-
task
-
context
-
taskState
-
processingInterval
private volatile long processingInterval
-
-
Constructor Details
-
TrcPeriodicThread
public TrcPeriodicThread(String instanceName, TrcPeriodicThread.PeriodicTask task, Object context, int taskPriority) Constructor: Create an instance of the object.- Parameters:
instanceName
- specifies the instance name.task
- specifies the periodic task the thread is to execute.context
- specifies the task context to be passed to the periodic thread.taskPriority
- specifies the periodic thread priority.
-
TrcPeriodicThread
Constructor: Create an instance of the object.- Parameters:
instanceName
- specifies the instance name.task
- specifies the periodic task the thread is to execute.context
- specifies the task context to be passed to the periodic thread.
-
-
Method Details
-
toString
This method returns the instance name. -
setRobotInitialized
public static void setRobotInitialized(boolean init) This method is called by the framework scheduler to inform us whether robotInit has finished. Periodic threads won't be executing until robotInit has finished.- Parameters:
init
- specifies true if robotInit has finished, false otherwise.
-
getNumActiveThreads
public static int getNumActiveThreads()This method returns the number of active threads.- Returns:
- number of active threads.
-
terminateTask
public void terminateTask()This method is called to terminate the periodic task. Once this is called, no other method in this class should be called except for isTaskTerminated(). -
isTaskTerminated
public boolean isTaskTerminated()This method checks if the periodic task has been terminated.- Returns:
- true if periodic task is terminated, false otherwise.
-
setTaskEnabled
public void setTaskEnabled(boolean enabled) This method enables/disables the periodic task. As long as the task is enabled, it will continue to acquire/process data.- Parameters:
enabled
- specifies true to enable periodic task, false to disable.
-
isTaskEnabled
public boolean isTaskEnabled()This method returns the state of the periodic task.- Returns:
- true if the periodic task is enabled, false otherwise.
-
setProcessingInterval
public void setProcessingInterval(long interval) This method sets the periodic task processing interval.- Parameters:
interval
- specifies the processing interval in msec. If 0, process as fast as the CPU can run.
-
getProcessingInterval
public long getProcessingInterval()This method returns the periodic task processing interval.- Returns:
- periodic task processing interval in msec. If zero, the task is run as fast as the CPU can go.
-
setData
This method is called to set new data after new data have been acquired/processed.- Parameters:
data
- specifies newly acquired/processed data.
-
getData
This method returns the acquired/processed data. If nothing found, it returns null.- Returns:
- acquired/processed data, null if nothing found.
-
run
public void run()This method runs the periodic processing task.
-