Class SubMonitor
- All Implemented Interfaces:
IProgressMonitor,IProgressMonitorWithBlocking
- Direct Known Subclasses:
ProbingSubMonitor
progress monitor that uses a given amount of work ticks from a parent monitor.
This is intended as a safer, easier-to-use alternative to SubProgressMonitor.
Progress monitoring is generally quite invasive to the code that is monitored. At the same time progress monitoring itself is typically very hard to implement correctly. This class aims at reducing the invasiveness as much as possible while offering all the functionality needed to do the job right.
The following aspects of this class help to keep the progress monitoring code short and nice and to avoid common monitoring mistakes:
- It offers the full functionality of
SubMonitor, which already makes progress monitoring a lot easier. Refer to theSubMonitordocumentation or to this article for details and examples. - In addition to the
setWorkRemaining(int)method it offers askipped(int)method, which redistributes the remaining work according to the last skippedworked(int)ornewChild(int)call rather than on the sum of all subsequent calls. - It reduces the need to specify
workarguments by using the default value1with the overloadedworked(),skipped()andnewChild()calls. - Basically all methods of this class can implicitly check for cancelation, thereby ensuring that the monitored code is always cancelable by the user
without cluttering the code with repetitions of the following idiom:
if (monitor.isCanceled()) { throw new OperationCanceledException(); }For details about automatic cancelation detection refer todetectCancelation(). - It is normally very challenging to find out how much time a program really spends in the different parts of the monitored methods or how often these
parts get executed. Stepping through the program with a debugger obviously leads to distortion that renders the observations meaningless and adding
extra code to measure a runtime scenario realistically is not nice from a maintenance point of view.
As a solution to this problem this class offers the possibility to transparently instrument
SubMonitorinstances such that they automatically collect and report all kinds of statistics that may help to enhance the user experience. Sometimes it would even indicate to remove some progress monitoring because it turns out that almost no time is being spent in a particular part of the program. Another typical result from the analysis is the understanding of one time effects that might need special consideration.For details about this probing mode refer to
ProbingSubMonitor.
The following example shows how to monitor progress while recursing through a tree of folders:
public void recurse(IContainer container, IProgressMonitor monitor) throws Exception
{
IResource[] members = container.members();
SubMonitor progress = SubMonitor.convert(monitor, members.length).detectCancelation();
progress.subTask(container.getFullPath().toString());
for (IResource member : members)
{
if (member instanceof IContainer)
{
Thread.sleep(5);
recurse((IContainer)member, progress.newChild());
}
else
{
progress.skipped();
}
}
}
- Since:
- 3.4
- Author:
- Eike Stepper
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumerates the possible probing mode valuesSubMonitor.ProbingMode.OFF,SubMonitor.ProbingMode.STANDARDandSubMonitor.ProbingMode.FULL. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intMay be passed as a flag to newChild.static final intMay be passed as a flag to newChild.static final intMay be passed as a flag to newChild.static final intMay be passed as a flag to newChild.static final intMay be passed as a flag to newChild.Fields inherited from interface org.eclipse.core.runtime.IProgressMonitor
UNKNOWN -
Method Summary
Modifier and TypeMethodDescriptionfinal voidStarts a new main task.voidfinal voidstatic SubMonitorconvert(IProgressMonitor monitor) Converts an unknown (possibly null) IProgressMonitor into a SubMonitor.static SubMonitorconvert(IProgressMonitor monitor, int work) Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated with the given number of ticks.static SubMonitorconvert(IProgressMonitor monitor, int work, SubMonitor.ProbingMode probingMode) Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated with the given number of ticks.static SubMonitorconvert(IProgressMonitor monitor, String taskName, int work) Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated with the given number of ticks.static SubMonitorconvert(IProgressMonitor monitor, String taskName, int work, SubMonitor.ProbingMode probingMode) Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated with the given number of ticks.static SubMonitorconvert(IProgressMonitor monitor, SubMonitor.ProbingMode probingMode) Converts an unknown (possibly null) IProgressMonitor into a SubMonitor.final SubMonitorfinal SubMonitordetectCancelation(boolean on) voiddone()protected static booleanfinal voidinternalWorked(double work) final booleanfinal SubMonitornewChild()Same asnewChild(1).final SubMonitornewChild(int totalWork) Creates a sub progress monitor that will consume the given number of ticks from the receiver.final SubMonitornewChild(int totalWork, int suppressFlags) Creates a sub progress monitor that will consume the given number of ticks from the receiver.final voidsetBlocked(IStatus reason) final voidsetCanceled(boolean b) final voidsetTaskName(String name) final SubMonitorsetWorkRemaining(int workRemaining) Sets the work remaining for this SubMonitor instance.final voidskipped()Same asskipped(1).final voidskipped(int ticks) final voidfinal voidworked()Same asworked(1).voidworked(int work) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.eclipse.core.runtime.IProgressMonitor
slice
-
Field Details
-
DEFAULT_WORK
public static final int DEFAULT_WORK- See Also:
-
SUPPRESS_SUBTASK
public static final int SUPPRESS_SUBTASKMay be passed as a flag to newChild. Indicates that the calls to subTask on the child should be ignored. Without this flag, calling subTask on the child will result in a call to subTask on its parent.- See Also:
-
SUPPRESS_BEGINTASK
public static final int SUPPRESS_BEGINTASKMay be passed as a flag to newChild. Indicates that strings passed into beginTask should be ignored. If this flag is specified, then the progress monitor instance will accept null as the first argument to beginTask. Without this flag, any string passed to beginTask will result in a call to setTaskName on the parent.- See Also:
-
SUPPRESS_SETTASKNAME
public static final int SUPPRESS_SETTASKNAMEMay be passed as a flag to newChild. Indicates that strings passed into setTaskName should be ignored. If this string is omitted, then a call to setTaskName on the child will result in a call to setTaskName on the parent.- See Also:
-
SUPPRESS_ALL_LABELS
public static final int SUPPRESS_ALL_LABELSMay be passed as a flag to newChild. Indicates that strings passed to setTaskName, subTask, and beginTask should all be ignored.- See Also:
-
SUPPRESS_NONE
public static final int SUPPRESS_NONEMay be passed as a flag to newChild. Indicates that strings passed to setTaskName, subTask, and beginTask should all be propagated to the parent.- See Also:
-
-
Method Details
-
convert
Converts an unknown (possibly null) IProgressMonitor into a SubMonitor. It is not necessary to call done() on the result, but the caller is responsible for calling done() on the argument. Calls beginTask on the argument.
This method should generally be called at the beginning of a method that accepts an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.
- Parameters:
monitor- monitor to convert to a SubMonitor instance or null. Treats null as a new instance ofNullProgressMonitor.- Returns:
- a SubMonitor instance that adapts the argument
-
convert
Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated with the given number of ticks. It is not necessary to call done() on the result, but the caller is responsible for calling done() on the argument. Calls beginTask on the argument.
This method should generally be called at the beginning of a method that accepts an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.
- Parameters:
monitor- monitor to convert to a SubMonitor instance or null. Treats null as a new instance ofNullProgressMonitor.work- number of ticks that will be available in the resulting monitor- Returns:
- a SubMonitor instance that adapts the argument
-
convert
Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated with the given number of ticks. It is not necessary to call done() on the result, but the caller is responsible for calling done() on the argument. Calls beginTask on the argument.
This method should generally be called at the beginning of a method that accepts an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.
- Parameters:
monitor- to convert into a SubMonitor instance or null. If given a null argument, the resulting SubMonitor will not report its progress anywhere.taskName- user readable name to pass to monitor.beginTask. Never null.work- initial number of ticks to allocate for children of the SubMonitor- Returns:
- a new SubMonitor instance that is a child of the given monitor
-
convert
Converts an unknown (possibly null) IProgressMonitor into a SubMonitor. It is not necessary to call done() on the result, but the caller is responsible for calling done() on the argument. Calls beginTask on the argument.
This method should generally be called at the beginning of a method that accepts an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.
- Parameters:
monitor- monitor to convert to a SubMonitor instance or null. Treats null as a new instance ofNullProgressMonitor.- Returns:
- a SubMonitor instance that adapts the argument
-
convert
public static SubMonitor convert(IProgressMonitor monitor, int work, SubMonitor.ProbingMode probingMode) Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated with the given number of ticks. It is not necessary to call done() on the result, but the caller is responsible for calling done() on the argument. Calls beginTask on the argument.
This method should generally be called at the beginning of a method that accepts an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.
- Parameters:
monitor- monitor to convert to a SubMonitor instance or null. Treats null as a new instance ofNullProgressMonitor.work- number of ticks that will be available in the resulting monitor- Returns:
- a SubMonitor instance that adapts the argument
-
convert
public static SubMonitor convert(IProgressMonitor monitor, String taskName, int work, SubMonitor.ProbingMode probingMode) Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated with the given number of ticks. It is not necessary to call done() on the result, but the caller is responsible for calling done() on the argument. Calls beginTask on the argument.
This method should generally be called at the beginning of a method that accepts an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.
- Parameters:
monitor- to convert into a SubMonitor instance or null. If given a null argument, the resulting SubMonitor will not report its progress anywhere.taskName- user readable name to pass to monitor.beginTask. Never null.work- initial number of ticks to allocate for children of the SubMonitor- Returns:
- a new SubMonitor instance that is a child of the given monitor
-
detectCancelation
-
detectCancelation
-
setWorkRemaining
Sets the work remaining for this SubMonitor instance. This is the total number of ticks that may be reported by all subsequent calls to worked(int), newChild(int), etc. This may be called many times for the same SubMonitor instance. When this method is called, the remaining space on the progress monitor is redistributed into the given number of ticks.
It doesn't matter how much progress has already been reported with this SubMonitor instance. If you call setWorkRemaining(100), you will be able to report 100 more ticks of work before the progress meter reaches 100%.
- Parameters:
workRemaining- total number of remaining ticks- Returns:
- the receiver
-
isCanceled
public final boolean isCanceled()- Specified by:
isCanceledin interfaceIProgressMonitor
-
setTaskName
- Specified by:
setTaskNamein interfaceIProgressMonitor
-
beginTask
Starts a new main task. The string argument is ignored if and only if the SUPPRESS_BEGINTASK flag has been set on this SubMonitor instance.This method is equivalent calling setWorkRemaining(...) on the receiver. Unless the SUPPRESS_BEGINTASK flag is set, this will also be equivalent to calling setTaskName(...) on the parent.
- Specified by:
beginTaskin interfaceIProgressMonitor- Parameters:
name- new main task nametotalWork- number of ticks to allocate- See Also:
-
done
public void done()- Specified by:
donein interfaceIProgressMonitor
-
internalWorked
public final void internalWorked(double work) - Specified by:
internalWorkedin interfaceIProgressMonitor
-
subTask
- Specified by:
subTaskin interfaceIProgressMonitor
-
worked
public final void worked()Same asworked(1). -
worked
public void worked(int work) - Specified by:
workedin interfaceIProgressMonitor
-
skipped
public final void skipped(int ticks) -
skipped
public final void skipped()Same asskipped(1). -
setCanceled
public final void setCanceled(boolean b) - Specified by:
setCanceledin interfaceIProgressMonitor
-
newChild
Same asnewChild(1). -
newChild
Creates a sub progress monitor that will consume the given number of ticks from the receiver. It is not necessary to call
beginTaskordoneon the result. However, the resulting progress monitor will not report any work after the first call to done() or before ticks are allocated. Ticks may be allocated by calling beginTask or setWorkRemaining.Each SubMonitor only has one active child at a time. Each time newChild() is called, the result becomes the new active child and any unused progress from the previously-active child is consumed.
This is property makes it unnecessary to call done() on a SubMonitor instance, since child monitors are automatically cleaned up the next time the parent is touched.
//////////////////////////////////////////////////////////////////////////// // Example 1: Typical usage of newChild void myMethod(IProgressMonitor parent) { SubMonitor progress = SubMonitor.convert(parent, 100); doSomething(progress.newChild(50)); doSomethingElse(progress.newChild(50)); } //////////////////////////////////////////////////////////////////////////// // Example 2: Demonstrates the function of active children. Creating children // is sufficient to smoothly report progress, even if worked(...) and done() // are never called. void myMethod(IProgressMonitor parent) { SubMonitor progress = SubMonitor.convert(parent, 100); for (int i = 0; i < 100; i++) { // Creating the next child monitor will clean up the previous one, // causing progress to be reported smoothly even if we don't do anything // with the monitors we create progress.newChild(1); } } //////////////////////////////////////////////////////////////////////////// // Example 3: Demonstrates a common anti-pattern void wrongMethod(IProgressMonitor parent) { SubMonitor progress = SubMonitor.convert(parent, 100); // WRONG WAY: Won't have the intended effect, as only one of these progress // monitors may be active at a time and the other will report no progress. callMethod(progress.newChild(50), computeValue(progress.newChild(50))); } void rightMethod(IProgressMonitor parent) { SubMonitor progress = SubMonitor.convert(parent, 100); // RIGHT WAY: Break up method calls so that only one SubMonitor is in use at a time. Object someValue = computeValue(progress.newChild(50)); callMethod(progress.newChild(50), someValue); }- Parameters:
totalWork- number of ticks to consume from the receiver- Returns:
- new sub progress monitor that may be used in place of a new SubMonitor
-
newChild
Creates a sub progress monitor that will consume the given number of ticks from the receiver. It is not necessary to call
beginTaskordoneon the result. However, the resulting progress monitor will not report any work after the first call to done() or before ticks are allocated. Ticks may be allocated by calling beginTask or setWorkRemaining.Each SubMonitor only has one active child at a time. Each time newChild() is called, the result becomes the new active child and any unused progress from the previously-active child is consumed.
This is property makes it unnecessary to call done() on a SubMonitor instance, since child monitors are automatically cleaned up the next time the parent is touched.
//////////////////////////////////////////////////////////////////////////// // Example 1: Typical usage of newChild void myMethod(IProgressMonitor parent) { SubMonitor progress = SubMonitor.convert(parent, 100); doSomething(progress.newChild(50)); doSomethingElse(progress.newChild(50)); } //////////////////////////////////////////////////////////////////////////// // Example 2: Demonstrates the function of active children. Creating children // is sufficient to smoothly report progress, even if worked(...) and done() // are never called. void myMethod(IProgressMonitor parent) { SubMonitor progress = SubMonitor.convert(parent, 100); for (int i = 0; i < 100; i++) { // Creating the next child monitor will clean up the previous one, // causing progress to be reported smoothly even if we don't do anything // with the monitors we create progress.newChild(1); } } //////////////////////////////////////////////////////////////////////////// // Example 3: Demonstrates a common anti-pattern void wrongMethod(IProgressMonitor parent) { SubMonitor progress = SubMonitor.convert(parent, 100); // WRONG WAY: Won't have the intended effect, as only one of these progress // monitors may be active at a time and the other will report no progress. callMethod(progress.newChild(50), computeValue(progress.newChild(50))); } void rightMethod(IProgressMonitor parent) { SubMonitor progress = SubMonitor.convert(parent, 100); // RIGHT WAY: Break up method calls so that only one SubMonitor is in use at a time. Object someValue = computeValue(progress.newChild(50)); callMethod(progress.newChild(50), someValue); }- Parameters:
totalWork- number of ticks to consume from the receiver- Returns:
- new sub progress monitor that may be used in place of a new SubMonitor
-
childDone
public void childDone() -
clearBlocked
public final void clearBlocked()- Specified by:
clearBlockedin interfaceIProgressMonitor
-
setBlocked
- Specified by:
setBlockedin interfaceIProgressMonitor
-
eq
-