Hi,
On Nov 25, 6:04 am, Hong Jiang <[email protected]> wrote:
> I'm new to Clojure and playing with small programs. Today I wrote a
> snippet to figure out how future works:
>
> (defn testf []
> (let [f (future #(do
> (Thread/sleep 5000)
> %)
> 5)
> g 7]
> (+ g @f)))
>
> (println (testf))
>
> I'm expecting the program to sleep 5 seconds and then print 12 and
> immediately terminate. However, the program seems to print out 12
> immediately, and it takes a long time (more like 50 seconds instead of
> 5) to terminate. Am I missing something?
future takes a body not a function. So in the new thread, you create a
new anonymous function which is immediately thrown away and 5 is
returned. This explains why 12 is printed immediately. The shutdown
time might be connected to something like shutdown-agents. But I'm not
sure on this one. To test the hypothesis you can do something like
this:
(defn testf []
(let [f (future
#(do
(Thread/sleep 5000)
(+ % 6))
5)
g 7]
(+ g @f)))
This should print still 12 and not 18.
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en