Prefer ThreadLocalRandom over Random

简介:   Java 7 has introduced a new random number generator - ThreadLocalRandomNormally to generate Random numbers, we either do Create an instance of java.

 

Java 7 has introduced a new random number generator - ThreadLocalRandom

Normally to generate Random numbers, we either do

However in a concurrent applications usage of above leads to contention issues -

  • Random is thread safe for use by multiple threads. But if multiple threads use the same instance of Random, the same seed is shared by multiple threads. It leads to contention between multiple threads and so to performance degradation. 
ThreadLocalRandom is solution to above problem. ThreadLocalRandom has a Random instance per thread and safeguards against contention.
 
From the api docs - 
Usages of this class should typically be of the form:  ThreadLocalRandom.current().nextX(...) (where  X is  IntLong, etc). When all usages are of this form, it is never possible to accidently share a  ThreadLocalRandom across multiple threads.
Usage Example - 
 
//Generate a random number b/w 0 and 10.  0 <= R < 10
//Using Math.random()
int r1 = (int)Math.random()*10;
//Using Random
Random rand = new Random();
int r2 = rand.nextInt(10);
//Using ThreadLocalRandom
int r3 = ThreadLocalRandom.current().nextInt(10);

 http://thoughtfuljava.blogspot.com/2012/09/prefer-threadlocalrandom-over-random.html

http://www.blogjava.net/yongboy/archive/2012/02/04/369574.html

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < 10000; i++) {
            int nextInt = ThreadLocalRandom.current().nextInt(10);
            if (nextInt >= 0) {
                String positive = "positive";
                Integer current = map.get(positive);
                map.put(positive, current == null ? 1 : ++current);
            } else {
                System.out.println(nextInt);
                String positive = "negative";
                Integer current = map.get(positive);
                map.put(positive, current == null ? 1 : ++current);
            }
        }
        System.out.println(map);

    }

 

相关文章
|
5月前
|
Oracle Java 关系型数据库
Random和ThreadLocalRandom区别
Random和ThreadLocalRandom区别
63 3
|
5月前
|
算法 C#
54.c#:random类
54.c#:random类
125 1
|
2月前
|
Java
成随机数的几种方法、Math.random()随机数的生成、Random()的使用
这篇文章介绍了生成随机数的三种方法:使用`System.currentTimeMillis()`获取当前时间的毫秒值来生成0到100的随机整数、使用`Math.random()`生成[0,1)范围内的`double`类型随机数并扩大到指定范围、以及使用`Random`对象的`nextInt()`方法生成指定范围内的随机整数,并提供了相应的Java代码示例和测试结果。
成随机数的几种方法、Math.random()随机数的生成、Random()的使用
|
3月前
|
安全 算法 Java
使用Random.next生成随机数
使用Random.next生成随机数
|
5月前
random.random()
random.random()
53 1
|
5月前
random.randint(a, b)
random.randint(a, b)
56 1
|
JavaScript 前端开发
Math.random();
Math.random();
91 0
|
算法 Java C++
JAVA中生成随机数Random VS ThreadLocalRandom性能比较
JAVA中生成随机数Random VS ThreadLocalRandom性能比较
225 0
JAVA中生成随机数Random VS ThreadLocalRandom性能比较
Random类和Math.random生成的随机数
Random类和Math.random生成的随机数
204 0