Package trclib.dataprocessor
Class TrcRequestQueue<R>
java.lang.Object
trclib.dataprocessor.TrcRequestQueue<R>
- Type Parameters:
R
- specifies the type of the request.
This class implements a generic request queue that runs on its own thread. It allows the caller to add requests
to the end of the queue. The request thread will call the client to process the request asynchronously from the
head of the queue. When the request is completed, an optional event will be signaled as well as an optional
callback if provided.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
This class implements a request entry. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private final String
private boolean
private TrcRequestQueue<R>.RequestEntry
private final LinkedBlockingQueue<TrcRequestQueue<R>.RequestEntry>
private Thread
private double
private int
private final TrcDbgTrace
-
Constructor Summary
ConstructorsConstructorDescriptionTrcRequestQueue
(String instanceName) Constructor: Creates an instance of the object. -
Method Summary
Modifier and TypeMethodDescriptionThis method queues a request at the end of the request queue to be processed asynchronously on a thread.addPriorityRequest
(R request, TrcEvent event) This method adds the priority request to the head of the queue.boolean
cancelRequest
(TrcRequestQueue<R>.RequestEntry entry) This method cancels a request.boolean
This method checks if the request queue is enabled.private void
This method is called when the request queue thread is started.void
setEnabled
(boolean enabled) This method enables/disables the request queue.void
setPerformanceTracingEnabled
(boolean enabled) This method enables/disables performance report.toString()
This method returns the instance name.
-
Field Details
-
tracer
-
instanceName
-
requestQueue
-
requestThread
-
enabled
private boolean enabled -
priorityRequest
-
perfTracingEnabled
private boolean perfTracingEnabled -
totalNanoTime
private double totalNanoTime -
totalRequests
private int totalRequests
-
-
Constructor Details
-
TrcRequestQueue
Constructor: Creates an instance of the object.- Parameters:
instanceName
- specifies the instance name.
-
-
Method Details
-
toString
This method returns the instance name. -
setEnabled
public void setEnabled(boolean enabled) This method enables/disables the request queue. On enable, it creates the request thread to start processing request entries in the queue. On disable, it shuts down the request thread and cancels all pending requests still in the queue.- Parameters:
enabled
- specifies true to enable request queue, false to disable.
-
isEnabled
public boolean isEnabled()This method checks if the request queue is enabled.- Returns:
- true if request queue is enabled, false if disabled.
-
setPerformanceTracingEnabled
public void setPerformanceTracingEnabled(boolean enabled) This method enables/disables performance report.- Parameters:
enabled
- specifies true to enable performance tracing, false to disable.
-
add
This method queues a request at the end of the request queue to be processed asynchronously on a thread.- Parameters:
request
- specifies the request to be queued.event
- specifies the event to notify when the request is up for processing.repeat
- specifies true to re-queue the request when completed.- Returns:
- request entry added to the end of the queue. It can be used to cancel the request if it is still in queue.
-
addPriorityRequest
This method adds the priority request to the head of the queue. It will be processed once the current active request is done processing. If there is already an existing priority request pending, this request will not be added to the queue and null is returned.- Parameters:
request
- specifies the priority request.event
- specifies the event to notify when the request is up for processing.- Returns:
- request entry added to the head of the queue. It can be used to cancel the request if it is still in queue. It may return null if the priority request failed to be added to the queue.
-
cancelRequest
This method cancels a request.- Parameters:
entry
- specifies the request entry from add or addPriorityRequest to be canceled.- Returns:
- true if the request entry is found in the queue and canceled, false otherwise.
-
requestTask
private void requestTask()This method is called when the request queue thread is started. It processes all entries in the request queue when they arrive. If the request queue is empty, the thread is blocked until a new request arrives. Therefore, this thread only runs when there are requests in the queue. If this thread is interrupted, it will clean up the request queue before exiting.
-