On Tue, Nov 23, 2010 at 11:45 PM, HiHeelHottie <hiheelhot...@gmail.com> wrote:
>
> Does anybody know of an implementation for a priority queue that can
> be used for scheduling events in the future?  I would like to put a
> map associated with a timestamp into the queue and be able to pull out
> all maps at or before a given time in order.

That's something you can slap together in a few minutes.

user=> (def sched (ref (sorted-map )))

(def jobber (agent nil))

(defn schedule-job!* [time runnable]
  (dosync
    (alter sched assoc time runnable)))

(defn- next-job-time []
  (key (first @sched)))

(defn- do-next-job! []
  (dosync
    (when-let [[k v] (first @sched)]
      (alter sched dissoc k)
      (send-off jobber (fn [_] (v) nil)))))

(defn- job-runner []
  (loop []
    (if-not (empty? @sched)
      (if (> (System/currentTimeMillis) (next-job-time))
        (do-next-job!)))
    (Thread/sleep 10)
    (recur)))

(doto (Thread. job-runner)
  (.setDaemon true)
  (.start))

(defn schedule-job-after!* [delay runnable]
  (schedule-job!* (+ (System/currentTimeMillis) delay) runnable))

(defmacro schedule-job! [time & body]
  `(schedule-job!* ~time (fn [] ~...@body)))

(defmacro schedule-job-after! [delay & body]
  `(schedule-job-after!* ~delay (fn [] ~...@body)))

(schedule-job-after! 10000
  (println "foo"))

(schedule-job-after! 30000
  (println "bar"))

(schedule-job-after! 20000
  (println "baz"))
{1290576825292
 #<user$eval2986$fn__2987 user$eval2986$fn__2...@1ef2fd3>,
 1290576835296
 #<user$eval2994$fn__2995 user$eval2994$fn__2...@1d5e94f>,
 1290576845294
 #<user$eval2990$fn__2991 user$eval2990$fn__2...@24b943>}
user=>
foo
baz
bar

The last three lines will print at ten-second intervals and may not
appear in the same console as your user=> prompt depending on how your
REPL/IDE handles asynchronous printlns. With NetBeans they'll appear
in *out* instead of Repl (for sure) and in slime in the inferior-lisp
buffer (I think).

This could be reworked to use java.util.Date but the basic
architecture would remain the same.

You'll need to (shutdown-agents) during shutdown of any application
where you use this if you don't want the JVM to linger as a zombie
process.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to