Class TimeGuard
java.lang.Object
com.igormaznitsa.meta.common.utils.TimeGuard
Allows to detect violations of execution time for code blocks or just measure time for them. It works separately for every Thread through ThreadLocal and check stack depth to be
informed about current operation level.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Interface for any object to be informed about time alerts.static final class
Data container for time watching action. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TimeGuard.TimeAlertListener
Some variant of "null-device" for time alerts, it does absolutely nothing.private static final ThreadLocal
<List<TimeGuard.TimeData>> Inside thread local storage of registered processors. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Add a time watcher.static void
addGuard
(String alertMessage, long maxAllowedDelayInMilliseconds, TimeGuard.TimeAlertListener timeAlertListener) Add a time watcher and provide processor of time violation.static void
addPoint
(String timePointName, TimeGuard.TimeAlertListener listener) Add a named time point.static void
cancel()
Cancel all time watchers and time points for the current stack level.static void
Cancel all time watchers and time points globally for the current thread.static void
check()
Check all registered time watchers for time bound violations.static void
checkPoint
(String timePointName) Check named time point(s).static void
Process all time points for the current stack level.static boolean
isEmpty()
Check that the thread local for the current thread contains time points or watchers.
-
Field Details
-
NULL_TIME_ALERT_LISTENER
Some variant of "null-device" for time alerts, it does absolutely nothing.- Since:
- 1.0
-
REGISTRY
Inside thread local storage of registered processors.- Since:
- 1.0
-
-
Constructor Details
-
TimeGuard
private TimeGuard()
-
-
Method Details
-
addGuard
@Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void addGuard(String alertMessage, @Constraint("X>0") long maxAllowedDelayInMilliseconds) Add a time watcher. As target of notification meta error listeners will be used.- Parameters:
alertMessage
- message for time violationmaxAllowedDelayInMilliseconds
- max allowed delay in milliseconds for executing block- Since:
- 1.0
- See Also:
-
addPoint
@Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void addPoint(String timePointName, TimeGuard.TimeAlertListener listener) Add a named time point.- Parameters:
timePointName
- name for the time pointlistener
- listener to be notified- Since:
- 1.0
- See Also:
-
checkPoint
@Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void checkPoint(String timePointName) Check named time point(s). Listener registered for the point will be notified and the point will be removed.- Parameters:
timePointName
- the name of time point- Since:
- 1.0
-
checkPoints
@Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void checkPoints()Process all time points for the current stack level.- Since:
- 1.0
-
addGuard
@Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void addGuard(String alertMessage, @Constraint("X>0") long maxAllowedDelayInMilliseconds, TimeGuard.TimeAlertListener timeAlertListener) Add a time watcher and provide processor of time violation.- Parameters:
alertMessage
- message for time violationmaxAllowedDelayInMilliseconds
- max allowed delay in milliseconds for executing blocktimeAlertListener
- alert listener to be notified, if it is null then the global one will get notification- Since:
- 1.0
- See Also:
-
cancelAll
Cancel all time watchers and time points globally for the current thread.- Since:
- 1.0
- See Also:
-
cancel
@Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void cancel()Cancel all time watchers and time points for the current stack level.- Since:
- 1.0
- See Also:
-
check
@Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void check()Check all registered time watchers for time bound violations.- Since:
- 1.0
- See Also:
-
isEmpty
@Weight(value=NORMAL, comment="May create list in thread local storage") public static boolean isEmpty()Check that the thread local for the current thread contains time points or watchers.- Returns:
- true if the thread local storage is empty, false otherwise
-