If you have this:

user> (def f (future (Thread/sleep 20000) :done))
#'user/f
user> @f  ; this immediate deref blocks for 20 sec, finally returning :block
:done
user> @f  ; returns immediately
:done

What is actually happening when you call the first @f? You are waiting
for the function to finish executing and return a value? Or are you
waiting for a return value to appear in the ref slot?

Is there any way to check if there is a value ready or if the function
has returned w/out blocking?



On Thu, Nov 26, 2009 at 12:32 AM, Hong Jiang <h...@hjiang.net> wrote:
> Thanks. Yeah, after adding a call to shutdown-agents, the process no
> longer hangs.
>
> On Nov 25, 1:15 pm, Kevin Downey <redc...@gmail.com> wrote:
>> future also uses the same threadpool as agents, so once you call
>> future the threadpool spins up, and just sort of sits around for a
>> while before the jvm decides to exit, which is why the program would
>> sit around for 50 seconds
>>
>>
>>
>> On Wed, Nov 25, 2009 at 10:30 AM, Hong Jiang <h...@hjiang.net> wrote:
>> > Thanks for your replies David and Sean. Yes, I made a mistake thinking
>> > that future takes a function and its arguments, so the function was
>> > never called in my program.
>>
>> > On Nov 25, 8:21 am, David Brown <cloj...@davidb.org> wrote:
>> >> On Tue, Nov 24, 2009 at 09:04:38PM -0800, Hong Jiang wrote:
>> >> >Hi all,
>>
>> >> >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)))
>>
>> >> You don't ever evaluate the function containing the sleep, you just
>> >> create it, and then immediately return 5.
>>
>> >> Future contains an explicit do, so you can just do the steps in the
>> >> future:
>>
>> >>      [f (future
>> >>           (Thread/sleep 5000)
>> >>          5)
>> >>          ...
>>
>> >> Or, if you want to get the function call in, you'll need to call it:
>>
>> >>      [f (future (#(do (Thread/sleep 5000) %) 5)) ...]
>>
>> >> David
>>
>> > --
>> > 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
>>
>> --
>> And what is good, Phaedrus,
>> And what is not good—
>> Need we ask anyone to tell us these things?
>
> --
> 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

-- 
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