Class StatisticsThread
- All Implemented Interfaces:
Runnable,Future<Void>,RunnableFuture<Void>
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:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class javax.swing.SwingWorker
SwingWorker.StateValueNested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<UUID, AtomicInteger> A thread-safe map to store the named counters.private static final ArrayList<StatisticsThread> The consumer that will receive the statistics map on the EDT for UI updates. -
Constructor Summary
ConstructorsConstructorDescriptionStatisticsThread(Consumer<Map<UUID, Integer>> uiUpdater) Constructs a new StatisticsThread. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclearAll()protected VoidThe main background task.voidAtomically increments the value of a registered statistic by one.protected voidReceives data chunks from thepublishmethod and executes on the EDT.Registers a new statistic to be tracked.voidRegisters a new statistic to be tracked.Methods inherited from class javax.swing.SwingWorker
addPropertyChangeListener, cancel, done, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, publish, removePropertyChangeListener, run, setProgressMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.concurrent.Future
exceptionNow, resultNow, state
-
Field Details
-
stats
A thread-safe map to store the named counters. Outside classes can access these counters to increment them from any thread. -
uiUpdater
The consumer that will receive the statistics map on the EDT for UI updates. -
threads
-
-
Constructor Details
-
StatisticsThread
Constructs a new StatisticsThread.- Parameters:
uiUpdater- AConsumerthat 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
Registers a new statistic to be tracked.- Returns:
- The stat's UUID.
-
registerStatistic
Registers a new statistic to be tracked.- Parameters:
id- The id of the new statistic.
-
increment
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
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 theprocess(java.util.List<java.util.Map<java.util.UUID, java.lang.Integer>>)method.- Specified by:
doInBackgroundin classSwingWorker<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
Receives data chunks from thepublishmethod 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.
-