java for循环执行得太快会导致System.currentTimeMillis()重复 [英] java for loop executes too fast gives System.currentTimeMillis() duplicate

查看:1237
本文介绍了java for循环执行得太快会导致System.currentTimeMillis()重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java:我有一个使用System.currentTimeMillis()函数的问题

我使用System.currentTimeMillis()在foor循环中产生唯一值问题是循环执行太快,System.currentTimeMillis()给我重复的值。

我怎样才能产生确定的唯一值。

< pre $ for(int a = 0; a< = 10; a ++){
System.out.println(System.currentTimeMillis())
}

我也试过,但是也不能生成唯一的数字

  System.currentTimeMillis()+ Math.random()


System.nanoTime()来代替?


Java: I have a problem using System.currentTimeMillis() function

i am using System.currentTimeMillis() to generate unique values in foor loop problem is loop executes too fast and System.currentTimeMillis() gives me duplicate values.

How can i generate for sure unique values.

for(int a=0;a<=10;a++){
System.out.println(System.currentTimeMillis())
}

I also tried following but it is also not generaet to generate unique number

System.currentTimeMillis()+Math.random()

解决方案

why don't you use System.nanoTime() instead?

这篇关于java for循环执行得太快会导致System.currentTimeMillis()重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆