今天时间学习ThreadLocalRandom api,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充
package test.java.util.concurrent;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.Test;
/**
* ThreadLocalRandom的测试类
*
* @date 2020-07-28 22:14:27
*/
public class ThreadLocalRandomTest {
/**
* 获取当前线程对应的ThreadLocalRandom
* @Param
*/
@Test
public void testCurrent()throws Exception{
System.out.println(ThreadLocalRandom.current().doubles().count());
}
/**
* 设置随机数生成器的种子
* @Param
*/
@Test
public void testSetSeed()throws Exception{
ThreadLocalRandom.current().setSeed(333);
}
/**
*获取下一个随机整数
* @Param
*/
@Test
public void testNextInt()throws Exception{
System.out.println(ThreadLocalRandom.current().nextInt());
}
/**
*获取下一个随机整数,指定上限
* @Param
*/
@Test
public void testNextInt1()throws Exception{
System.out.println(ThreadLocalRandom.current().nextInt(3));
}
/**
* 获取下一个随机整数,指定下限和上限
* @Param
*/
@Test
public void testNextInt2()throws Exception{
System.out.println(ThreadLocalRandom.current().nextInt(3, 4));
}
/**
*获取下一个随机长整数
* @Param
*/
@Test
public void testNextLong()throws Exception{
System.out.println(ThreadLocalRandom.current().nextLong());
}
/**
*获取下一个随机长整数,指定上限
* @Param
*/
@Test
public void testNextLong1()throws Exception{
System.out.println(ThreadLocalRandom.current().nextLong(3));
}
/**
*获取下一个随机整数,指定下限和上限
* @Param
*/
@Test
public void testNextLong2()throws Exception{
System.out.println(ThreadLocalRandom.current().nextLong(3, 4));
}
/**
*获取下一个随机double
* @Param
*/
@Test
public void testNextDouble()throws Exception{
System.out.println(ThreadLocalRandom.current().nextDouble());
}
/**
*获取下一个随机double,指定上限
* @Param
*/
@Test
public void testNextDouble1()throws Exception{
System.out.println(ThreadLocalRandom.current().nextDouble(3));
}
/**
*获取下一个随机double,指定下限和上限
* @Param
*/
@Test
public void testNextDouble2()throws Exception{
System.out.println(ThreadLocalRandom.current().nextDouble(3, 4));
}
/**
* 获取随机boolean值
* @Param
*/
@Test
public void testNextBoolean()throws Exception{
System.out.println(ThreadLocalRandom.current().nextBoolean());
}
/**
*获取随机boolean值
* @Param
*/
@Test
public void testNextFloat()throws Exception{
System.out.println(ThreadLocalRandom.current().nextFloat());
}
/**
*返回下一个伪随机数
* @Param
*/
@Test
public void testNextGaussian()throws Exception{
System.out.println(ThreadLocalRandom.current().nextGaussian());
}
/**
* 获取指定大小的int处理流
* @Param
*/
@Test
public void testInts()throws Exception{
System.out.println(ThreadLocalRandom.current().ints(3L).count());
}
/**
* 获取int处理流
* @Param
*/
@Test
public void testInts1()throws Exception{
System.out.println(ThreadLocalRandom.current().ints().count());
}
/**
*获取指定大小的int处理流,指定最低和最高值
* @Param
*/
@Test
public void testInts2()throws Exception{
System.out.println(ThreadLocalRandom.current().ints(3,3,3).count());
}
/**
*获取int处理流,指定最低和最高值
* @Param
*/
@Test
public void testInts3()throws Exception{
System.out.println(ThreadLocalRandom.current().ints(3,3).count());
}
/**
*获取指定大小的long处理流
* @Param
*/
@Test
public void testLongs()throws Exception{
System.out.println(ThreadLocalRandom.current().longs(3L).count());
}
/**
*获取long处理流
* @Param
*/
@Test
public void testLongs1()throws Exception{
System.out.println(ThreadLocalRandom.current().longs().count());
}
/**
*获取指定大小的long处理流,指定最低和最高值
* @Param
*/
@Test
public void testLongs2()throws Exception{
System.out.println(ThreadLocalRandom.current().longs(3,3,3).count());
}
/**
*获取long处理流,指定最低和最高值
* @Param
*/
@Test
public void testLongs3()throws Exception{
System.out.println(ThreadLocalRandom.current().longs(3,3).count());
}
/**
*获取指定大小的double处理流
* @Param
*/
@Test
public void testDoubles()throws Exception{
System.out.println(ThreadLocalRandom.current().doubles(3L).count());
}
/**
*获取double处理流
* @Param
*/
@Test
public void testDoubles1()throws Exception{
System.out.println(ThreadLocalRandom.current().doubles().count());
}
/**
*获取指定大小的double处理流,指定最低和最高值
* @Param
*/
@Test
public void testDoubles2()throws Exception{
System.out.println(ThreadLocalRandom.current().doubles(3,3,3).count());
}
/**
*获取double处理流,指定最低和最高值
* @Param
*/
@Test
public void testDoubles3()throws Exception{
System.out.println(ThreadLocalRandom.current().doubles(3,3).count());
}
}