HRTimer is a simple system-wide timer facility using a singleton Timer, with additional instrumentation. : Timer « Development Class « Java






HRTimer is a simple system-wide timer facility using a singleton Timer, with additional instrumentation.

      
/**
 * Copyright (c) 2006 Richard Rodgers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package com.monad.homerun.util;

import java.util.Timer;
import java.util.TimerTask;

/**
 * HRTimer is a simple system-wide timer facility using a singleton Timer,
 * with additional instrumentation.
 */

public class HRTimer
{
    // the singleton instance
    private static HRTimer instance = null;

    // the timer instance
    private Timer hrTimer = null;

    // task counters
    private int numRepeatTasks = 0;

    private int numOneOffTasks = 0;

    // private constructor
    private HRTimer()
    {
        hrTimer = new Timer( true );
    }

    /**
     * Resturns an instance of an HRTimer
     * 
     * @return timer
     *         the timer instance
     */
    public static synchronized HRTimer getInstance()
    {
        if ( instance == null )
        {
            instance = new HRTimer();
        }
        return instance;
    }

    /**
     * Adds a repeating task to the timer
     * 
     * @param task
     *        the timer task to add
     * @param period
     *        the time interval between task executions 
     */
    public void addTask( TimerTask task, long period )
    {
        ++numRepeatTasks;
        hrTimer.schedule( task, 0L, period );
    }

    /**
     * Adds a task to be executed only once
     * 
     * @param task
     *        the task to perform
     * @param delay
     *        initial time interval before execution
     */
    public void addOneOffTask( TimerTask task, long delay )
    {
        ++numOneOffTasks;
        hrTimer.schedule( task, delay );
    }

    /**
     * Adds a repeating task with an initial delay
     * 
     * @param task
     *        the task to add
     * @param delay
     *        the intial delay before first execution
     * @param period
     *        the interval between task executions
     */
    public void addDelayedTask( TimerTask task, long delay, long period )
    {
        ++numRepeatTasks;
        hrTimer.schedule( task, delay, period );
    }

    /**
     * Cancels a regsitered task
     * 
     * @param task
     *        the task to be cancelled
     */
    public void cancelTask( TimerTask task )
    {
        --numRepeatTasks;
        task.cancel();
    }

    /**
     * Returns the number of active tasks
     * 
     * @return num
     *         the number of current tasks
     */
    public int getNumTasks()
    {
        return numRepeatTasks + numOneOffTasks;
    }

    /**
     * Returns the current number of repeatable tasks
     * 
     * @return num
     *         the number of repreatable tasks
     */
    public int getNumRepeatTasks()
    {
       return numRepeatTasks;
    }

    /**
     * Returns the current number of non-repeatable tasks
     * 
     * @return num
     *         the number of non-repreatable tasks
     */
    public int getNumOneOffTasks()
    {
        return numOneOffTasks;
    }
}

   
    
    
    
    
    
  








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.Class encapsulating timer functionality
16.Timeout Observer
17.A pool of objects that should only be used by one thread at a time
18.A class that allows a programmer to determine the amount of time spend doing certain routines
19.All times in this Timer object are in milliseconds. The timer starts from the moment of it's creation.
20.Thread Timer
21.Cancel Aware Timer
22.Used for timing events with millisecond precision.
23.A class to wait until a condition becomes true.