在java中生成唯一ID的最佳方法是什么?人们一般使用
String id = System.currentTimeMillis+ someStaticCounter;
但这种方法需要在多线程应用程序中进行同步。
我在用
try
{
Thread.sleep(1);
//This sleep ensures that two consecutive calls from the same thread does not return the same id.
}
catch (InterruptedException e)
{
// do nothing;
}
id = System.currentTimeMillis() + "-" + Thread.currentThread().getId();
这种方法可以帮助我减少同步开销。
请问有更好的方法吗?
UUID 怎么样:http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html#randomUUID%28%29
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)