Class TrcRequestQueue<R>

java.lang.Object
trclib.dataprocessor.TrcRequestQueue<R>
Type Parameters:
R - specifies the type of the request.

public class TrcRequestQueue<R> extends Object
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.
  • Field Details

    • tracer

      private final TrcDbgTrace tracer
    • instanceName

      private final String instanceName
    • requestQueue

      private final LinkedBlockingQueue<TrcRequestQueue<R>.RequestEntry> requestQueue
    • requestThread

      private volatile Thread requestThread
    • enabled

      private boolean enabled
    • priorityRequest

      private TrcRequestQueue<R>.RequestEntry priorityRequest
    • perfTracingEnabled

      private boolean perfTracingEnabled
    • totalNanoTime

      private double totalNanoTime
    • totalRequests

      private int totalRequests
  • Constructor Details

    • TrcRequestQueue

      public TrcRequestQueue(String instanceName)
      Constructor: Creates an instance of the object.
      Parameters:
      instanceName - specifies the instance name.
  • Method Details

    • toString

      public String toString()
      This method returns the instance name.
      Overrides:
      toString in class Object
      Returns:
      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

      public TrcRequestQueue<R>.RequestEntry add(R request, TrcEvent event, boolean repeat)
      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

      public TrcRequestQueue<R>.RequestEntry addPriorityRequest(R request, TrcEvent event)
      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

      public boolean cancelRequest(TrcRequestQueue<R>.RequestEntry entry)
      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.