- All Implemented Interfaces:
AutoCloseable,Runnable
- Direct Known Subclasses:
ConsoleProgressMonitor,IjProgressBarMonitor
This class represents a "progress monitor". This is a simple Thread that queries the attached target task
(implementing ProgressReporter) and calls its handleProgress(double, long) method at regular
intervals. ProgressMonitor implements the AutoCloseable and thus can (and should) be used in a
try-with-resources context, e.g.,
ProgressReporter task = ....; // the activity to be monitored
try (ProgressMonitor m = new ConsoleProgressMonitor(task)) {
// run task ...
}
Otherwise, if not used in a auto-start/close mode, the methods Thread.start() and close() should be used
to "manually" start and terminate monitoring. See ProgressMonitorExample, GenericFilter,
ConsoleProgressMonitor and IjProgressBarMonitor for examples.
Note that this mechanism cannot be used to monitor activities inside a constructor, since no reference to the monitored instance is available before the constructor is finished.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default wait time between successive progress queries.Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionProgressMonitor(ProgressReporter target) Constructor, starts monitoring immediately.ProgressMonitor(ProgressReporter target, boolean autoStart) Constructor, optionally starts monitoring immediately or waits for itsThread.start()method to be called (which starts the associated thread). -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()doubleReturns the time elapsed since monitoring started (in seconds).abstract voidhandleProgress(double progress, long elapsedNanoTime) Called periodically by theProgressMonitorthread supplying the current progress value (degree of completion).voidrun()voidsetWaitTime(int waitTime) The time interval between progress queries can be set with this method.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Field Details
-
DefaultWaitTime
The default wait time between successive progress queries.- See Also:
-
-
Constructor Details
-
ProgressMonitor
Constructor, starts monitoring immediately. The target object'sProgressReporter.getProgress()method is called it regular intervals.- Parameters:
target- the object (task) to be monitored
-
ProgressMonitor
Constructor, optionally starts monitoring immediately or waits for itsThread.start()method to be called (which starts the associated thread). The target object'sProgressReporter.getProgress()method is called it regular intervals.- Parameters:
target- the object (task) to be monitoredautoStart- starts immediately if true
-
-
Method Details
-
setWaitTime
The time interval between progress queries can be set with this method. See alsoDefaultWaitTime.- Parameters:
waitTime- the time interval (in milliseconds)
-
handleProgress
Called periodically by theProgressMonitorthread supplying the current progress value (degree of completion). It is up to the implementation what action should be performed, e.g., display the current progress graphically (see e.g.IjProgressBarMonitor).- Parameters:
progress- the current progress value (degree of completion) in [0,1)elapsedNanoTime- the time elapsed since this progress monitor was started (in nanoseconds)
-
run
-
close
- Specified by:
closein interfaceAutoCloseable
-
getElapsedTime
Returns the time elapsed since monitoring started (in seconds).- Returns:
- the elapsed time in seconds
-