PenProfile 2.x

com.livescribe.util
Class InactivityTimer

java.lang.Object
  extended by com.livescribe.util.InactivityTimer
All Implemented Interfaces:
Runnable

public abstract class InactivityTimer
extends Object
implements Runnable

This class implements a timer thread that can be used to monitor application activity. The abstract notifyNoActivity() method is called when no activity is detected for a specified inactivity time.

The timer is started with the startTimer(long, long) or startTimer(long) methods, and is stopped when there is either no activity for the specified timeout or stopTimer() is called.

A timer instance can be used more than once. To reuse the instance, the timer must be stopped. If the notifyNoActivity() or notifyStopped() methods are called, then the timer will be stopped. The isStopped() method can also be used to determine if the timer is stopped ("not running").

One useful way to use this class is either as an inner or anonymous class.


Constructor Summary
InactivityTimer()
          Creates a new inactivity timer.
 
Method Summary
 boolean isStopped()
          Indicates if the timer is stopped.
protected abstract  void notifyNoActivity()
          The method is called when there is no activity for the specified inactivity time.
protected  void notifyStopped()
          This is called when the timer has been stopped or interrupted.
 void recordActivity()
          This marks some activity at the current time and resets the timer's waiting period to be the initial inactivity time.
 void recordActivityIfRunning()
          This atomically combines the check for the timer running with marking some activity.
 void run()
          The timer thread code.
 void startTimer(long inactivityTime)
          Starts the timer using the specified inactivity period.
 void startTimer(long lastActivityTime, long inactivityTime)
          Starts the timer.
 void stopTimer()
          Stops the timer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InactivityTimer

public InactivityTimer()
Creates a new inactivity timer.

Method Detail

startTimer

public void startTimer(long lastActivityTime,
                       long inactivityTime)
Starts the timer. This version of the method allows a "last activity" time to be set in addition to the timeout period.

The timer is only started if it is not already running and if the inactivity time is greater than zero.

Parameters:
lastActivityTime - use this as the last time activity occurred, in ms
inactivityTime - the inactivity period, in ms
Throws:
java.lang.IllegalStateException - if the timer is already running.
See Also:
startTimer(long), isStopped()

startTimer

public void startTimer(long inactivityTime)
Starts the timer using the specified inactivity period. When this period expires, notifyNoActivity() is called.

The timer is only started if it is not already running and if the inactivity time is greater than zero.

Parameters:
inactivityTime - the inactivity period, in ms
Throws:
java.lang.IllegalStateException - if the timer is already running.
See Also:
notifyNoActivity(), isStopped()

stopTimer

public void stopTimer()
Stops the timer. If the timer has not already stopped then notifyStopped() will be called instead of notifyNoActivity(). Simply call startTimer(long) to restart the timer.

See Also:
notifyStopped()

isStopped

public boolean isStopped()
Indicates if the timer is stopped. This indicates if the thread had or is about to call one of notifyNoActivity() or notifyStopped().

Note that this does not necessarily indicate whether the timer has been stopped via stopTimer(). Rather, it is equivalent to Is this timer not still running?

Returns:
true if the timer is stopped and false otherwise.

run

public void run()
The timer thread code. This will disallow outside code to execute it.

Specified by:
run in interface Runnable
See Also:
Thread.run()

notifyNoActivity

protected abstract void notifyNoActivity()
The method is called when there is no activity for the specified inactivity time. If this method is called then notifyStopped() will not be called.

See Also:
startTimer(long), startTimer(long, long)

notifyStopped

protected void notifyStopped()
This is called when the timer has been stopped or interrupted. If this method is called then notifyNoActivity() will not be called.


recordActivity

public void recordActivity()
This marks some activity at the current time and resets the timer's waiting period to be the initial inactivity time. This has no effect if the timer is not running.

See Also:
startTimer(long)

recordActivityIfRunning

public void recordActivityIfRunning()
This atomically combines the check for the timer running with marking some activity. This avoids the problem of the small delay between an isStopped() check and a recordActivity() call.

Please see recordActivity() for more information.

Since:
2.4
See Also:
startTimer(long), recordActivity(), isStopped()

PenProfile 2.x

Copyright © 2010 Livescribe Inc. All Rights Reserved.
Confidential and subject to NDA.