Class ProgressMonitor

java.lang.Object
java.lang.Thread
imagingbook.common.util.progress.ProgressMonitor
All Implemented Interfaces:
AutoCloseable, Runnable
Direct Known Subclasses:
ConsoleProgressMonitor, IjProgressBarMonitor

public abstract class ProgressMonitor extends Thread implements AutoCloseable

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:
  • Field Details

  • Constructor Details

  • Method Details

    • setWaitTime

      public void setWaitTime(int waitTime)
      The time interval between progress queries can be set with this method. See also DefaultWaitTime.
      Parameters:
      waitTime - the time interval (in milliseconds)
    • handleProgress

      public abstract void handleProgress(double progress, long elapsedNanoTime)
      Called periodically by the ProgressMonitor thread 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

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • getElapsedTime

      public double getElapsedTime()
      Returns the time elapsed since monitoring started (in seconds).
      Returns:
      the elapsed time in seconds