On Aug 27, 2011, at 10:41 PM, Andy Fingerhut wrote:

> I suspect that (rand), which calls java.lang.Math.random(), is a synchronized 
> method, meaning that if you try to call it from many parallel threads, even 
> those on physically separate cores, will execute one at a time.  Things could 
> even take more time if you try to execute them in parallel than sequentially, 
> due to lock contention overhead that does not occur if you try to execute 
> them sequentially.
> 
> http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#random%28%29
> 
> Try doing something besides (rand) in your function t that does not use 
> locking or synchronized methods.  You might also experiment with having no 
> Thread/sleep call in there, but that might be part of what you are trying to 
> learn the behavior of.  I'm not sure.

Or if you really want to be getting random numbers in all of those threads you 
could arrange for all of them to use thread-local Random objects -- created 
with something like (new java.util.Random) -- and then get your random numbers 
with something like (. thread-local-random-generator (nextFloat)). I did 
something similar in my project at https://github.com/lspector/Clojush although 
there I use agents rather than pmap to initiate the concurrent processes.

 -Lee

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