Class encapsulating timer functionality : Timer « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Timer 




Class encapsulating timer functionality
    
/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 
 * Project: OpenSubsystems
 
 * $Id: MyTimer.java,v 1.5 2007/01/07 06:14:00 bastafidli Exp $
 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */


/**
 * Class encapsulating timer functionality.
 
 @version $Id: MyTimer.java,v 1.5 2007/01/07 06:14:00 bastafidli Exp $
 @author Miro Halas
 * @code.reviewer Miro Halas
 * @code.reviewed Initial revision
 */
public class MyTimer
{
   // Attributes ///////////////////////////////////////////////////////////////
   
   /**
    * Remeber start time here.
    */
   private long m_lStartTime;
   
   /**
    * Remeber stop time here.
    */
   private long m_lStopTime;

   // Constructors /////////////////////////////////////////////////////////////
   
   /**
    * Default constructor.
    * Starts counting fro the moment it is cosntructed.
    */
   public MyTimer(
   )
   {
      reset();
   }

   // Logic ////////////////////////////////////////////////////////////////////
   
   /**
    @return long - start time;
    */
   public long getStartTime()
   {
      return m_lStartTime;
   }

   /**
    @return - stop time;
    */
   public long getStopTime()
   {
      return m_lStopTime;
   }

   /**
    * Reset the counter and start counting from scratch.
    */
   public void reset(
   )
   {
      m_lStartTime = System.currentTimeMillis();
      m_lStopTime = 0;
   }

   /**
    * Stop the timer.
    */
   public void stop(
   )
   {
      m_lStopTime = System.currentTimeMillis();
   }

   /**
    * Get timer duration (the timer doesn't stop) in milliseconds.
    
    @return long - difference between stop and start time.
    */
   public long getDuration(
   )
   {
      long lStopTime;

      if (m_lStopTime == 0)
      {
         lStopTime = System.currentTimeMillis();
      }
      else
      {
         lStopTime = m_lStopTime;
      }

      return lStopTime - m_lStartTime;
   }

   /**
    * Print the state of the timer without stopping it.
    @return String - timing information
    */
   public String toString(
   )
   {
      long lTotalMS   = getDuration();
      long lMS        = lTotalMS % 1000;
      long lTotalSecs = lTotalMS / 1000;
      long lSecs      = lTotalSecs % 60;
      long lTotalMins = lTotalSecs / 60;
      long lMinutes   = lTotalMins % 60;
      long lHours     = lTotalMins / 60;
      StringBuffer sbBuffer = new StringBuffer();

      if (lHours > 0)
      {
         sbBuffer.append(lHours);
         sbBuffer.append(":");
         sbBuffer.append(lMinutes);
         sbBuffer.append(":");
         sbBuffer.append(lSecs);
         sbBuffer.append(".");
         sbBuffer.append(lMS);
      }
      else if (lMinutes > 0)
      {
         sbBuffer.append(lMinutes);
         sbBuffer.append(":");
         sbBuffer.append(lSecs);
         sbBuffer.append(".");
         sbBuffer.append(lMS);
      }
      else if (lSecs > 0)
      {
         sbBuffer.append(lSecs);
         sbBuffer.append(".");
         sbBuffer.append(lMS);
         sbBuffer.append(" seconds");
      }
      else
      {
         sbBuffer.append(lMS);
         sbBuffer.append(" ms");
      }
      
      return sbBuffer.toString();
   }
}

   
    
    
    
  














Related examples in the same category
1.Timer Skipping BeepTimer Skipping Beep
2.Timer Schedule a task that executes once every secondTimer Schedule a task that executes once every second
3.Use java.util.Timer to schedule a task to execute once 5 seconds have passedUse java.util.Timer to schedule a task to execute once 5 seconds have passed
4.Timer utilities
5.Timer and TimerTask Classes
6.Pause and start a timer task
7.Create a Timer object
8.Swing also provide a Timer class. A Timer object will send an ActionEvent to the registered ActionListener.
9.Schedule a task by using Timer and TimerTask.
10.Scheduling a Timer Task to Run Repeatedly
11.Create a scheduled task using timer
12.extends TimerTask to create your own task
13.A simple implementation of the Java 1.3 java.util.Timer API
14.Scheduling a Timer Task to Run at a Certain Time
15.Timeout Observer
16.A pool of objects that should only be used by one thread at a time
17.A class that allows a programmer to determine the amount of time spend doing certain routines
18.All times in this Timer object are in milliseconds. The timer starts from the moment of it's creation.
19.Thread Timer
20.Cancel Aware Timer
21.HRTimer is a simple system-wide timer facility using a singleton Timer, with additional instrumentation.
22.Used for timing events with millisecond precision.
23.A class to wait until a condition becomes true.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.