Prefer ThreadLocalRandom over Random

Java 7 has introduced a new random number generator - ThreadLocalRandom
Normally to generate Random numbers, we either do
- Create an instance of java.util.Random OR
- Math.random() - which internally creates an instance of java.util.Random on first invocation.
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.
Usages of this class should typically be of the form:
ThreadLocalRandom.current().nextX(...)(whereXisInt,Long, etc). When all usages are of this form, it is never possible to accidently share aThreadLocalRandomacross multiple threads.
//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);
}
Prefer ThreadLocalRandom over Random的更多相关文章
- ThreadLocalRandom ---- 提升Random在大并发下的效率
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 随机数 随机数在科学研究与工程实际中有着极其重要的应用! ...
- 为什么要使用ThreadLocalRandom代替Random生成随机数
799 java里有伪随机型和安全型两种随机数生成器,伪随机生成器根据特定公式将seed转换成新的伪随机数据的一部分,安全随机生成器在底层依赖到操作系统提供的随机事件来生成数据. 安全随机生成器 需要 ...
- Random类、ThreadLocalRandom类
Random和ThreadLocalRandom类均用于生成伪随机数. Random的构造函数: Random() 默认以系统当前时间为种子,相当于Random(System.currentT ...
- Math&Random&ThreadLocalRandom类
Math类 //绝对值值运算: Math.abs(18.999); //返回19.999这个数的绝对值 Math.abs(-12.58); // 返回-12.58这个数的绝对值,为12.58 //取值 ...
- JUC源码分析-其它工具类(一)ThreadLocalRandom
JUC源码分析-其它工具类(一)ThreadLocalRandom ThreadLocalRandom 是 JDK7 在 JUC 包下新增的随机数生成器,它解决了 Random 在多线程下多个线程竞争 ...
- 【8-22】java学习笔记04
java基础类库 Scanner类(java.util.scanner) Scanner对象.hasNextXxx(),hasNext()默认方法为字符串://Returns true if this ...
- 解密随机数生成器(二)——从java源码看线性同余算法
Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术 ...
- Java中基础类库使用
Java中基础类库: 在这里我仅仅介绍几种我个人觉得会常常使用的 1:Object类中的Clone机制仅仅是对对象进行浅层次的克隆,假设须要进行深层次的克隆的话那么就要自己写(详细Clone方法请參考 ...
- 常用 API
运行 Java 程序的参数.使用 Scanner 获取键盘输入.使用 BufferedReader 获取键盘输入.System类.Runtime类.Object类.Java 7新增的 Objects ...
随机推荐
- LeetCode之“字符串”:Valid Number(由此引发的对正则表达式的学习)
题目链接 题目要求: Validate if a given string is numeric. Some examples: "0" => true " 0.1 ...
- PS 图像调整算法——亮度调整
这个算法是参考自 阿发伯 的博客,在此对 阿发伯 表示感谢, http://blog.csdn.net/maozefa 亮度调整 非线性亮度调整: 对于R,G,B三个通道,每个通道增加相同的增量. 线 ...
- how tomcat works 总结 三
第七章 日志记录器 第 7 章包括日志,该组件是用来记录错误信息和其他信息的. 这一章比较简单,类图如下: 根据名字我想大家都能猜出来三个实现类都是做什么的,一个按常规输出到控制台,一个按错误模式输出 ...
- how tomcat works 总结 二
第五章 servlet容器 第 5 章讨论 container 模块.container 指的是 org.apache.catalina.Container 接口,有4 种类型的 container: ...
- HBase缓存的使用
hbase中的缓存分了两层:memstore和blockcache. 其中memstore供写使用,写请求会先写入memstore,regionserver会给每个region提供一个memstore ...
- unix下对于字符串变量的各种操作总结
在unix like系统的shell中,提供了很多操作字符串变量的灵活语法,我们接下来依次来看一看. apple@kissAir: ~$path=$PATH apple@kissAir: ~$echo ...
- Eclipse安装SVN插件(转载)
http://www.cnblogs.com/ruiati/p/3584120.html 1.下载最新的Eclipse,我的版本是3.7.2 indigo(Eclipse IDE for Java E ...
- 安装 Anaconda 的正确姿势
下面以 Anaconda2 安装为例, 说明如何更加流畅的使用 Conda Install Anaconda2 安装 Anaconda2(从清华源下载比较快) wget https://mirrors ...
- Day9 基于TCP的套接字和基于UDP的套接字
服务端: ss=socket() #创建服务器套接字 ss.bind() #把地址绑定到套接字 ss.listen() #监听套接字, inf_loop: #服务器无限循环 cs=ss.accept( ...
- VueJs(12)---vue-router(导航守卫,路由元信息)
vue-router(导航守卫,路由元信息) 之前泄露两篇有关vue-router博客: VueJs(10)---vue-router(进阶1) VueJs(11)---vue-router(进阶2) ...