Class StopWatch.Task

  • Enclosing class:
    StopWatch

    public static class StopWatch.Task
    extends java.lang.Object
    Represents a named task tracked by a StopWatch. Each task records its start time in nanoseconds and calculates the elapsed time when stop() is called.

    Tasks are compared by their taskName only, making task names unique within a single stop watch's running task list.

    Example Usage

    
       StopWatch.Task task = StopWatch.Task.start("parsing");
       // perform parsing work
       task.stop();
       System.out.println(task.getTaskName());    // "parsing"
       System.out.println(task.getElapsedNanos()); // elapsed time in nanoseconds
     
    Since:
    1.0.0
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)
      Compares this task with the specified object for equality.
      long getElapsedNanos()
      Returns the elapsed time of this task in nanoseconds.
      long getStartTimeNanos()
      Returns the start time of this task in nanoseconds, as reported by System.nanoTime().
      java.lang.String getTaskName()
      Returns the name of this task.
      int hashCode()
      Returns a hash code for this task, based on the task name.
      boolean isReentrant()
      Returns whether this task allows reentrant starts.
      static StopWatch.Task start​(java.lang.String taskName)
      Creates and starts a new non-reentrant task with the given name.
      static StopWatch.Task start​(java.lang.String taskName, boolean reentrant)
      Creates and starts a new task with the given name and reentrancy setting.
      void stop()
      Stops this task and records the elapsed time since it was started.
      java.lang.String toString()
      Returns a string representation of this task, including the task name and elapsed time in nanoseconds.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • start

        public static StopWatch.Task start​(java.lang.String taskName)
        Creates and starts a new non-reentrant task with the given name.

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("validation");
           // perform validation
           task.stop();
         
        Parameters:
        taskName - the name of the task
        Returns:
        a new running StopWatch.Task instance
      • start

        public static StopWatch.Task start​(java.lang.String taskName,
                                           boolean reentrant)
        Creates and starts a new task with the given name and reentrancy setting.

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("retryableOp", true);
           // perform the operation
           task.stop();
           System.out.println(task.isReentrant()); // true
         
        Parameters:
        taskName - the name of the task
        reentrant - true if the task may be started again while running
        Returns:
        a new running StopWatch.Task instance
      • stop

        public void stop()
        Stops this task and records the elapsed time since it was started.

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("encoding");
           // perform encoding
           task.stop();
           System.out.println("Elapsed: " + task.getElapsedNanos() + " ns");
         
      • getTaskName

        public java.lang.String getTaskName()
        Returns the name of this task.

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("sorting");
           String name = task.getTaskName(); // "sorting"
         
        Returns:
        the task name
      • isReentrant

        public boolean isReentrant()
        Returns whether this task allows reentrant starts.

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("retry", true);
           boolean reentrant = task.isReentrant(); // true
         
        Returns:
        true if the task is reentrant, false otherwise
      • getStartTimeNanos

        public long getStartTimeNanos()
        Returns the start time of this task in nanoseconds, as reported by System.nanoTime().

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("lookup");
           long startNanos = task.getStartTimeNanos();
         
        Returns:
        the start time in nanoseconds
      • getElapsedNanos

        public long getElapsedNanos()
        Returns the elapsed time of this task in nanoseconds. This value is zero until stop() has been called.

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("render");
           // perform rendering
           task.stop();
           long elapsed = task.getElapsedNanos();
           System.out.println("Render took " + elapsed + " ns");
         
        Returns:
        the elapsed time in nanoseconds, or 0 if the task has not been stopped
      • equals

        public boolean equals​(java.lang.Object o)
        Compares this task with the specified object for equality. Two tasks are considered equal if they have the same taskName.

        Example Usage

        
           StopWatch.Task task1 = StopWatch.Task.start("compile");
           StopWatch.Task task2 = StopWatch.Task.start("compile");
           boolean equal = task1.equals(task2); // true
         
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - the object to compare with
        Returns:
        true if the given object is a StopWatch.Task with the same name
      • hashCode

        public int hashCode()
        Returns a hash code for this task, based on the task name.

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("hash-demo");
           int code = task.hashCode();
         
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code of this task
      • toString

        public java.lang.String toString()
        Returns a string representation of this task, including the task name and elapsed time in nanoseconds.

        Example Usage

        
           StopWatch.Task task = StopWatch.Task.start("transform");
           task.stop();
           System.out.println(task.toString());
           // Task[name='transform', elapsed(ns)=...]
         
        Overrides:
        toString in class java.lang.Object
        Returns:
        a descriptive string representation of this task