Class ThreadCpuTimer


  • public class ThreadCpuTimer
    extends Object
    Allows tracking information about the current thread using the JVM's built-in management bean ThreadMXBean. Methods on this class are safe for use on any thread, but will return different values for different threads by design.
    • Method Detail

      • beginContext

        public static void beginContext​(String context)
      • isSupported

        public static boolean isSupported()
      • readNSAndReset

        public static Optional<Long> readNSAndReset​(String context)
        Get the number of nanoseconds since the last time this thread took a reading for the supplied context.
        Parameters:
        context - An arbitrary name that code can supply to avoid clashing with other usages.
        Returns:
        An optional long which may be empty if java.lang.management.ManagementFactory#getThreadMXBean() is unsupported or otherwise unavailable.
      • reset

        public static void reset​(String context)
        Discard any accumulated time for a given context since the last invocation.
        Parameters:
        context - the context to reset
      • reset

        public static void reset()
        Cleanup method. This should be called at the very end of a request thread when it's absolutely sure no code will attempt a new reading.