skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, September 20, 2012

Java Stopwatch Implementation

Posted by Admin at 12:05 PM – 0 comments
 

Java Stopwatch Implementation


public class StopWatch implements java.io.Serializable {

   private long startTime = -1;
   private long stopTime = -1;
   private boolean running = false;

   public StopWatch start() {
      startTime = System.currentTimeMillis();
      running = true;
      return this;
   }
   public StopWatch stop() {
      stopTime = System.currentTimeMillis();
      running = false;
      return this;
   }

   /** returns elapsed time in milliseconds 
     * if the watch has never been started then
     * return zero
     */
   public long getElapsedTime() {
      if (startTime == -1) {
         return 0;
      }
      if (running) {
         return System.currentTimeMillis()-startTime;
      } else {
         return stopTime - startTime; 
      }     
   }
  
   public StopWatch reset() {
      startTime = -1;
      stopTime = -1;
      running = false;
      return this;
   }
}





Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger