Moh*_*rid 3 java uniqueidentifier
在java中生成唯一ID的最佳方法是什么.人们通常使用
String id = System.currentTimeMillis+ someStaticCounter;
Run Code Online (Sandbox Code Playgroud)
但是这种方法需要在多线程应用程序中进行同步.
我在用
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();
Run Code Online (Sandbox Code Playgroud)
这种方法可以帮助我避免同步开销.
有什么更好的方法请建议吗?
| 归档时间: |
|
| 查看次数: |
2888 次 |
| 最近记录: |