为什么要使用ThreadLocalRandom代替Random生成随机数
- 799
java里有伪随机型和安全型两种随机数生成器,伪随机生成器根据特定公式将seed转换成新的伪随机数据的一部分,安全随机生成器在底层依赖到操作系统提供的随机事件来生成数据。
安全随机生成器
- 需要生成加密性强的随机数据的时候才用它
- 生成速度慢
- 如果需要生成大量的随机数据,可能会产生阻塞需要等待外部中断事件
而伪随机生成器,只依赖于“seed”的初始值,如果给生成算法提供相同的seed,可以得到一样的伪随机序列。一般情况下,由于它是计算密集型的(不依赖于任何IO设备),因此生成速度更快。以下是伪随机生成器的进化史。
java.util.Random
自1.0就已经存在,是一个线程安全类,理论上可以通过它同时在多个线程中获得互不相同的随机数,这样的线程安全是通过AtomicLong实现的。
Random使用AtomicLong CAS(compare and set)操作来更新它的seed,尽管在很多非阻塞式算法中使用了非阻塞式原语,CAS在资源高度竞争时的表现依然糟糕,后面的测试结果中可以看到它的糟糕表现。
java.util.concurrent.ThreadLocalRandom
1.7增加该类,企图将它和Random结合以克服所有的性能问题,该类继承自Random。
ThreadLocalRandom的主要实现细节:
- 使用一个普通的long而不是使用Random中的AtomicLong作为seed
- 不能自己创建ThreadLocalRandom实例,因为它的构造函数是私有的,可以使用它的静态工厂ThreadLocalRandom.current()
- 它是CPU缓存感知式的,使用8个long虚拟域来填充64位L1高速缓存行
测试
下面进行5种测试:
- 一个单独的Random被N个线程共享
ThreadLocal<Random>- ThreadLocalRandom
- Random[], 其中每个线程N使用一个数组下标为N的Random
- Random[], 其中每个线程N使用一个数组下标为N * 2的Random
所有的测试都使用封装在RandomTask类里的方法,每个方案都说明了如何使用随机生成器。
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom; public class Test_Random { private static final long COUNT = 10000000;
private static final int THREADS = 2;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Shared Random");
testRandom(THREADS, COUNT);
/*System.out.println("ThreadLocal<Random>");
testThreadLocal_Random(THREADS, COUNT);
System.out.println("ThreadLocalRandom");
testThreadLocalRandom(THREADS, COUNT);
System.out.println("Shared Random[] with no padding");
testRandomArray(THREADS, COUNT, 1);
System.out.println("Shared Random[] with padding");
testRandomArray(THREADS, COUNT, 2);*/
} private static class RandomTask implements Runnable {
private final Random rnd;
protected final int id;
private final long cnt;
private final CountDownLatch latch; private RandomTask(Random rnd, int id, long cnt,
CountDownLatch latch) {
super();
this.rnd = rnd;
this.id = id;
this.cnt = cnt;
this.latch = latch;
} protected Random getRandom() {
return rnd;
} @Override
public void run() {
try {
final Random r = getRandom();
latch.countDown();
latch.await();
final long start = System.currentTimeMillis();
int sum = 0;
for (long j = 0; j < cnt; j++) {
sum += r.nextInt();
}
final long time = System.currentTimeMillis() - start;
System.out.println("Thread #" + id + " Time = " + time / 1000.0 + " sec, sum = " + sum);
} catch (InterruptedException e) {}
}
} private static void testRandom(final int threads, final long cnt) {
final CountDownLatch latch = new CountDownLatch(threads);
final Random r = new Random(100);
for (int i = 0; i < threads; ++i) {
final Thread thread = new Thread(new RandomTask(r, i, cnt, latch));
thread.start();
}
} private static void testRandomArray(final int threads, final long cnt, final int padding) {
final CountDownLatch latch = new CountDownLatch(threads);
final Random[] rnd = new Random[threads * padding];
for (int i = 0; i < threads * padding; ++i) {
rnd[i] = new Random(100);
}
for (int i = 0; i < threads; ++i) {
final Thread thread = new Thread(new RandomTask(rnd[i * padding], i, cnt, latch));
thread.start();
}
} private static void testThreadLocalRandom(final int threads, final long cnt) {
final CountDownLatch latch = new CountDownLatch(threads);
for (int i = 0; i < threads; ++i) {
final Thread thread = new Thread(new RandomTask(null, i, cnt, latch) {
@Override
protected Random getRandom() {
// TODO Auto-generated method stub
return ThreadLocalRandom.current();
}
});
thread.start();
}
} private static void testThreadLocal_Random(final int threads, final long cnt) {
final CountDownLatch latch = new CountDownLatch(threads);
final ThreadLocal<Random> rnd = new ThreadLocal<Random>() { @Override
protected Random initialValue() {
// TODO Auto-generated method stub
return new Random(100);
} };
for (int i = 0; i < threads; ++i) {
final Thread thread = new Thread(new RandomTask(null, i, cnt, latch) { @Override
protected Random getRandom() {
// TODO Auto-generated method stub
return rnd.get();
} });
thread.start();
}
}
}
总结:
- 任何情况下都不要在多个线程间共享一个Random实例,而该把它放入ThreadLocal之中
- java7在所有情形下都更推荐使用ThreadLocalRandom,它向下兼容已有的代码且运营成本更低
为什么要使用ThreadLocalRandom代替Random生成随机数的更多相关文章
- C# random生成随机数全部一样
最近做排序测试 使用random生成随机数全部一样 估计是因为random采用的随机种子为时间戳 而一个循化执行消耗的时间没有到时间戳的最小单位 故没有变化 Thread.Sleep(10); 使用 ...
- centos 阶段复习 2015-4-6 dd命令 hosts.allow和hosts.deny 啊铭的myssh脚本 清空history命令历史 /dev/zero 零发生器 /dev/null 黑洞 /dev/random 生成随机数 第十一节课
centos 阶段复习 2015-4-6 dd命令 hosts.allow和hosts.deny 啊铭的myssh脚本 清空history命令历史 /dev/zero 零发生器 /dev/nul ...
- 【转载】python 模块 - random生成随机数模块
随机数种子 要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的: random.seed(1) 这样random.randint(0,6, (4,5)) ...
- Python3使用random生成随机数
本文介绍使用Python3中的random库生成随机数.随机小数.随机序列.随机字符串以及扑克洗牌等方法. 一.生成随机浮点数或小数 1.#生成0-1之间的浮点数 import random rnd ...
- Random 生成随机数
Random类 (java.util) Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要 ...
- Python random() 生成随机数
random() 函数中常见的函数如下: #!/usr/bin/python # -*- coding: UTF-8 -*- import random print( random.randint(1 ...
- C# Random循环生成随机数重复问题解决方案
C# Random循环生成随机数重复问题解决方案1.当我们通过Random生成随机数时,习惯的写法如下: int a=new Random().Next(0,100); 然后生成一个数据数没有任何问题 ...
- 生成随机数(Random类)和获取用户输入(Scanner类)
生成指定范围内的随机数 Math.random() 生成随机数,随机数在0到1之间,类型是 double. public class randCase { public static void mai ...
- 关于用 random 生成伪随机数的一个手笔
我在想还要不要写什么文字.确实不需要太多的文字描述吧. 前奏插一个小话题,之前在网上看到这样的冷笑话(有图的),一个程序猿调试个程序,早上怀疑某某地方的错误,下午怀疑某某地方的错误,晚上怀疑某某地方可 ...
随机推荐
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- Java集合之Hashtable
和HashMap一样,Hashtable也是一个散列表,存储的内容也是键值对key-value映射.它继承了Dictionary,并实现了Map.Cloneable.io.Serializable接口 ...
- equal与== 个人笔记
首先看看下面的图,看清楚了图咱们要说的知识点也就说说清楚了一半 int a=10; String b="ss"; String c=new String("kkk&quo ...
- java 项目得到jar和classes路径
java 项目得到jar和classes路径 public static String getJarPath(Class clazz) { String path = clazz.getProtect ...
- Linux备份策略(第二版)
备份策略 备份思想 一.系统潜在的威胁 Ø 系统硬件故障 Ø 软件故障 Ø 电源故障 Ø 用户的误操作 Ø 人为破坏 Ø 缓存中的内容没有及时的写入磁盘 Ø 自然灾害 二.备份介质的选择 备份介质:硬 ...
- 安卓TV开发(概述) 智能电视之视觉设计和体验分析
转载说明出处 :http://blog.csdn.net/sk719887916, 作者:skay 前言:移动智能设备的发展,推动了安卓另一个领域,包括智能电视和智能家居,以及可穿戴设备的大 ...
- android 应用模式之mvp
说到MVP就不得不提到MVC,做过J2EE的猿友们肯定知道MVC是个什么东西.MVC即 Model.View.Controller, 那MVP就Model.View.Presenter.Model用于 ...
- Spring 官网下载zip jar
第一步:打开官网:http://www.springsource.org/download/community: 第二步:点击图片 第三步:点击图标 第四步:找到如下链接,点击进去 第五步:再找到如下 ...
- 初探linux子系统集之led子系统(二)
巴西世界杯,德国7比1东道主,那个惨不忍睹啊,早上起来看新闻,第一眼看到7:1还以为点球也能踢成这样,后来想想,点球对多嘛6比1啊,接着就是各种新闻铺天盖地的来了.其实失败并没有什么,人生若是能够成功 ...
- 开发composer包,打通github和packagist,并自动更新
1. 首先需要本地安装好composer,并配置好环境变量,在命令行输入composer,显示以下信息就表示正常安装 2. 在github对应项目的根目录下进行初始化composer 初始化完成后,就 ...