Bruno P. Kinoshita commented on LANG-1254:

Just to complement on the issue description. Here's the mentioned text from 
Random's javadoc.

>Instances of java.util.Random are threadsafe. However, the concurrent use of 
>the same java.util.Random instance across threads may encounter contention and 
>consequent poor performance. Consider instead using ThreadLocalRandom in 
>multithreaded designs.

We already try to identify code that is threadsafe or not in \[lang\], and 
point it in the javadoc of some classes. Probably we have more code marked as 
threadsafe that could encounter contention and poor performance. We could try 
to further optimise that code, covering all possible scenarios of poor 
performance... but IMO it's easier to let the user choose which performance 
optimisations would be necessary.

So users would have control, and choose to use a ThreadLocal and maybe a 
wrapper object, or not. Writing simple, threadsafe code, I believe we are also 
giving more flexibility to users in choosing how to design their code for 
different environments, and use different optimisation techniques.

So I am neutral for replacing the Random instance by a ThreadLocalRandom (i.e. 
neither voting +0 nor -0, simply 0?). However, I think probably adding 
[~jmark]'s note about the performance issues and pointing users to Random 
javadocs would be a good idea.

What do you think [~jmark]?


> ThreadLocalRandom should be used in utility classes
> ---------------------------------------------------
>                 Key: LANG-1254
>                 URL: https://issues.apache.org/jira/browse/LANG-1254
>             Project: Commons Lang
>          Issue Type: Improvement
>            Reporter: John Mark
>            Priority: Minor
> I noticed that both {{RandomUtils}} and {{RandomStringUtils}} use a constant 
> for the {{Random}} instance. The javadoc for 
> [Random|http://docs.oracle.com/javase/8/docs/api/java/util/Random.html] 
> indicates that 
> [ThreadLocalRandom|http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadLocalRandom.html]
>  should be used for performance in multithreaded designs. I would think that 
> a public utility class would qualify as a multithreaded design and should 
> therefore use {{ThreadLocalRandom}}

This message was sent by Atlassian JIRA

Reply via email to