From 43684639074e2c68c197c9803670e49e1eed7316 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 5 Jun 2002 02:50:57 +0000 Subject: [PATCH] Created a SafeInterval that requeues itself on the client invoker thread (the main client thread, which will generally be the AWT thread unless the client is being used in a situation where there is no AWT) to avoid bad thread clashy. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1424 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/client/util/SafeInterval.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/java/com/threerings/presents/client/util/SafeInterval.java diff --git a/src/java/com/threerings/presents/client/util/SafeInterval.java b/src/java/com/threerings/presents/client/util/SafeInterval.java new file mode 100644 index 000000000..dbf19c22c --- /dev/null +++ b/src/java/com/threerings/presents/client/util/SafeInterval.java @@ -0,0 +1,55 @@ +// +// $Id: SafeInterval.java,v 1.1 2002/06/05 02:50:57 mdb Exp $ + +package com.threerings.presents.client.util; + +import com.samskivert.util.Interval; +import com.samskivert.util.IntervalManager; + +import com.threerings.presents.client.Client; + +/** + * Used in conjunction with the {@link Client}, this class provides a + * means by which code can be run on the client main thread at some point + * in the future, either as a recurring interval or as a one shot deal. + * The code is built on top of the {@link IntervalManager} services. + * + *

A {@link SafeInterval} instance should be created and then + * scheduled to run using the {@link IntervalManager}. For example: + * + *

+ *     IntervalManager.register(new SafeInterval(_ctx.getClient()) {
+ *         public void run () {
+ *             System.out.println("Foo!");
+ *         }
+ *      }, 25L * 1000L, null, false);
+ * 
+ */ +public abstract class SafeInterval + implements Runnable, Interval +{ + /** + * Creates a safe interval instance that will queue itself up for + * execution using the supplied client when it expires. + */ + public SafeInterval (Client client) + { + _client = client; + } + + /** + * Called (on the client main thread) when the interval period has + * expired. If this is a recurring interval, this method will be + * called each time the interval expires. + */ + public abstract void run (); + + /** Handles the proper scheduling and queueing. */ + public void intervalExpired (int id, Object arg) + { + _client.getInvoker().invokeLater(this); + } + + /** The client via which we queue ourselves when we expire. */ + protected Client _client; +}