The laziness of map is irrelevant here, because .invokeAll is treating it 
as a collection and realizing it all immediately. Your version with for is 
just as lazy. What's actually changed is that your map and for produce 
entirely different outputs: the version with map calls test-fn, and the 
version with for produces a function which, when invoked, will call 
test-fn. Since the latter is what the original code you copied from does, 
of course only that version works. You could change your map to (map #(fn 
[] (test-fn % 1 5)) deliveries) as an easy fix, though personally I 
wouldn't use the #() syntax here; its closeness to a (fn) form is a little 
confusing. (map (fn [i] (fn [] (test-fn i 1 5))) deliveries) is equivalent, 
of course. Personally I prefer the version with the for-comprehension.

On Friday, June 14, 2013 2:30:04 AM UTC-7, Amir Wasim wrote:
>
> I was using the first example to make a process executing parallely
>
>
> (defn test-stm [nitems nthreads niters]
>   (let [refs  (map ref (repeat nitems 0))
>         pool  (Executors/newFixedThreadPool nthreads)
>         tasks (map (fn [t]
>                       (fn []
>                         (dotimes [n niters]
>                           (dosync
>                             (doseq [r refs]
>                               (alter r + 1 t))))))
>                    (range nthreads))]
>     (doseq [future (.invokeAll pool tasks)]
>       (.get future))
>     (.shutdown pool)
>     (map deref refs)))
>
>  
>
> i was generating taks just like above as follows
>
>        tasks (map #(test-fn % 1 5) deliveries)
>
> But only one task was active at a time, although Executors was configured 
> with 4 threads. It occurred to me that map itself is lazy and it is 
> realized in doseq one at a time. A possible fix is to use for instead of 
> map to generate tasks
>
> I tried with the following
>
>        tasks (for [delivery_ deliveries] #(test-fn delivery_ 1 5))
>
> and it works and 4 threads are active during execution.
>
>
>
>

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to