Thanks. I looked at Java doc for random which says
"This method is properly synchronized to allow correct use by more
than one thread. However, if many threads need to generate
pseudorandom numbers at a great rate, it may reduce contention for
each thread to have its own pseudorandom-number generator."

art


Returns:


On Aug 27, 9:22 pm, Lee Spector <lspec...@hampshire.edu> wrote:
> 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#...
>
> > 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 
> athttps://github.com/lspector/Clojushalthough 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