[ 
https://issues.apache.org/jira/browse/HTRACE-170?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14540516#comment-14540516
 ] 

Colin Patrick McCabe commented on HTRACE-170:
---------------------------------------------

Hi [~vladimirsitnikov],

I think there was a really early version of Java (maybe it was jdk4?) that did 
have a non-threadsafe Random, but that is a thing of the past.  The JavaDoc for 
{{java.util.Random}} says:
{code}
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.
{code}

So it does work, but perhaps is slower than it might be.

It sounds like this could be improved!  Would you like to create a patch 
replacing these usages with a thread-local alternative?

> Non-thread safe use of Random
> -----------------------------
>
>                 Key: HTRACE-170
>                 URL: https://issues.apache.org/jira/browse/HTRACE-170
>             Project: HTrace
>          Issue Type: Bug
>    Affects Versions: 3.3.0
>            Reporter: Vladimir Sitnikov
>
> org.apache.htrace.Tracer#nonZeroRandom64 is obviously shares the same Random 
> instance between multiple execution threads.
> That does not work.
> Either {{ThreadLocal<Random>}} (1.5+) or {{ThreadLocalRandom}} (1.7+) should 
> be used.
> Other places with the same problem:
> org.apache.htrace.impl.CountSampler#random
> org.apache.htrace.impl.MilliSpan#nonZeroRandom64
> org.apache.htrace.impl.ProbabilitySampler#next



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to