[concurrency-interest] ThreadLocalRandom

Doug Lea dl at cs.oswego.edu
Tue Jan 13 07:30:41 EST 2009


jason marshall wrote:
> In what situation you would intentionally share a single Random object 
> among multiple threads?

Right. We all know this. But apparently many people look at
Random class, notice that it is thread-safe, and decide to
use a global one. And there are a few cases
where it is a reasonable choice - for example when
creating a static Random that provides seeds for local ones.
(ConcurrentSkipListMap does something along these lines.)

But we'd rather not penalize people who use Random the
normally best way, thread-locally, with crummy performance
(see my last mail).

> 
> Or perhaps to put it another way, what makes a Random instance any more 
> special than the dozens of other objects I'm likely to be accidentally 
> sharing between threads, that I'd want to solve the problem differently 
> than I would the other 11 cases?
> 

Random is somewhat special in that (1) overhead and contention
can cost a lot more than the underlying functionality (2) Many
programs that use Random use it a lot, so it is a known performance
bottleneck.

You are right that some programs out there include other classes
with similar problems. But we don't know what classes these are.
We can at least address the ones we know about.

-Doug



More information about the Concurrency-interest mailing list