背景
公司服务陆续接入sonar代码质量检测扫描,并且集成了p3c规则,在一个红包服务中使用了Random来产生随机数,扫描提示如下:
Creating a new Random object each time a random value is needed is
inefficient and may produce numbers which are not random depending on
the JDK. For better efficiency and randomness, create a single Random,
then store, and reuse it.
建议使用SecureRandom,于是根据建议本地(Windows)修改测试没问题后,代码跟着正常迭代发生产。
踩坑:
这个看似正常简单的用法替换,却隐藏着一个大隐患。修改的代码发布后,陆续收到一些请求处理报错: