Class StatisticsThread

java.lang.Object
javax.swing.SwingWorker<Void,Map<UUID,Integer>>
com.j3d.threads.StatisticsThread
All Implemented Interfaces:
Runnable, Future<Void>, RunnableFuture<Void>

public class StatisticsThread extends SwingWorker<Void,Map<UUID,Integer>>
A background thread that collects and reports statistics to the Swing Event Dispatch Thread (EDT) once per second.

This class uses a SwingWorker to perform a long-running task (collecting stats) on a background thread, preventing the UI from freezing. It safely publishes the collected data to the EDT for UI updates.

The statistics are stored as thread-safe AtomicIntegers, allowing any outside class to increment them at any time without causing concurrency issues.

See Also:
  • Field Details

    • stats

      private final Map<UUID,AtomicInteger> stats
      A thread-safe map to store the named counters. Outside classes can access these counters to increment them from any thread.
    • uiUpdater

      private final Consumer<Map<UUID,Integer>> uiUpdater
      The consumer that will receive the statistics map on the EDT for UI updates.
    • threads

      private static final ArrayList<StatisticsThread> threads
  • Constructor Details

    • StatisticsThread

      public StatisticsThread(Consumer<Map<UUID,Integer>> uiUpdater)
      Constructs a new StatisticsThread.
      Parameters:
      uiUpdater - A Consumer that will be executed on the EDT each second, receiving a map of the latest statistics to update the UI.
  • Method Details

    • clearAll

      public static void clearAll()
    • registerStatistic

      public UUID registerStatistic()
      Registers a new statistic to be tracked.
      Returns:
      The stat's UUID.
    • registerStatistic

      public void registerStatistic(UUID id)
      Registers a new statistic to be tracked.
      Parameters:
      id - The id of the new statistic.
    • increment

      public void increment(UUID id)
      Atomically increments the value of a registered statistic by one. This method is thread-safe and can be called from any thread.
      Parameters:
      id - The id of the statistic to increment.
    • doInBackground

      protected Void doInBackground() throws Exception
      The main background task. This method runs on a worker thread, not the EDT.

      It enters a loop that, once per second, captures the current value of all statistic counters, resets them to zero, and then publishes the captured values to the process(java.util.List<java.util.Map<java.util.UUID, java.lang.Integer>>) method.

      Specified by:
      doInBackground in class SwingWorker<Void,Map<UUID,Integer>>
      Returns:
      null, as this worker does not return a final result.
      Throws:
      InterruptedException - if the thread is interrupted while sleeping.
      Exception
    • process

      protected void process(List<Map<UUID,Integer>> chunks)
      Receives data chunks from the publish method and executes on the EDT.

      This method is guaranteed to run on the Event Dispatch Thread, making it safe to update Swing components. It calls the UI updater with the latest statistics.

      Overrides:
      process in class SwingWorker<Void,Map<UUID,Integer>>
      Parameters:
      chunks - A list of statistics maps published from the background thread. In this implementation, it will typically contain one map per second.