Modified safe interval to use IntervalManager services because there's a

single instance of the IntervalManager unlike the standard Java Timer
services which would require someone to have a timer instance to do the
scheduling of our safe intervals. Plus Ray will be much happier now that
the SafeInterval is not so flippant about reusing methods.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1392 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-05-24 21:38:24 +00:00
parent 989a9867e0
commit 8a7a046df8
@@ -1,10 +1,10 @@
// //
// $Id: SafeInterval.java,v 1.1 2002/05/24 21:03:15 mdb Exp $ // $Id: SafeInterval.java,v 1.2 2002/05/24 21:38:24 mdb Exp $
package com.threerings.presents.server.util; package com.threerings.presents.server.util;
import java.util.Timer; import com.samskivert.util.Interval;
import java.util.TimerTask; import com.samskivert.util.IntervalManager;
import com.threerings.presents.server.PresentsDObjectMgr; import com.threerings.presents.server.PresentsDObjectMgr;
@@ -12,22 +12,21 @@ import com.threerings.presents.server.PresentsDObjectMgr;
* Used in conjunction with the {@link PresentsDObjectMgr}, this class * Used in conjunction with the {@link PresentsDObjectMgr}, this class
* provides a means by which code can be run on the dobjmgr thread at some * provides a means by which code can be run on the dobjmgr thread at some
* point in the future, either as a recurring interval or as a one shot * point in the future, either as a recurring interval or as a one shot
* deal. The code is built on top of the standard Java {@link Timer} * deal. The code is built on top of the {@link IntervalManager} services.
* services.
* *
* <p> A {@link SafeInterval} instance should be created and then * <p> A {@link SafeInterval} instance should be created and then
* scheduled to run using the standard {@link Timer} services. For * scheduled to run using the {@link IntervalManager}. For example:
* example:
* *
* <pre> * <pre>
* Timer.schedule(new SafeInterval(_omgr) { * IntervalManager.register(new SafeInterval(_omgr) {
* public void intervalExpired () { * public void run () {
* System.out.println("Foo!"); * System.out.println("Foo!");
* } * }
* }, 25L * 1000L); * }, 25L * 1000L, null, false);
* </pre> * </pre>
*/ */
public abstract class SafeInterval extends TimerTask public abstract class SafeInterval
implements Runnable, Interval
{ {
/** /**
* Creates a safe interval instance that will queue itself up for * Creates a safe interval instance that will queue itself up for
@@ -41,44 +40,16 @@ public abstract class SafeInterval extends TimerTask
/** /**
* Called (on the dobjmgr thread) when the interval period has * Called (on the dobjmgr thread) when the interval period has
* expired. If this is a recurring interval, this method will be * expired. If this is a recurring interval, this method will be
* called each time the interval expires (until {@link #cancel} is * called each time the interval expires.
* called on this instance).
*/ */
public abstract void intervalExpired (); public abstract void run ();
/** Handles the proper scheduling and queueing. */ /** Handles the proper scheduling and queueing. */
public void run () public void intervalExpired (int id, Object arg)
{ {
boolean flipped = isFlipped(); _omgr.postUnit(this);
flip();
if (flipped) {
intervalExpired();
} else {
_omgr.postUnit(this);
}
}
/** Returns whether or not we're running on the timer thread or the
* dobjmgr thread. It is synchronized because the variable that tracks
* this state is inspected on both threads. */
protected synchronized boolean isFlipped ()
{
return _flipped;
}
/** Flips our mode (see {@link #isFlipped}). It is synchronized
* because the variable that tracks this state is updated on both
* threads. */
public synchronized void flip ()
{
_flipped = !_flipped;
} }
/** The dobjmgr on which we queue ourselves when we expire. */ /** The dobjmgr on which we queue ourselves when we expire. */
protected PresentsDObjectMgr _omgr; protected PresentsDObjectMgr _omgr;
/** Because {@link #run} is called both when the interval expires and
* when the dobjmgr invokes us, we have to use this toggle to figure
* out when to do what. */
protected boolean _flipped;
} }