1
我正在尝试生成从0
开始的唯一自动增量编号。 我找到了这样做使用下面的代码:使用Java生成自动增量编号
private static final AtomicInteger count = new AtomicInteger(0);
uniqueID = count.incrementAndGet();
我可以手动创建自动递增ID:
例如:
int i = 0; // Obviously I will declare this variable as global
++i;
这样,我可以管理要做到这一点。但我不想手动执行此操作。我找到了一些java方法,它将采用所有这些东西。
而且我发现下面的代码:
private static final AtomicInteger count = new AtomicInteger(0);
uniqueID = count.incrementAndGet();
现在的问题是,这是产生唯一的ID以正确的方式?或者,还有其他更好的解决方案吗?
这只适用于单进程环境。 – shmosel
这将只在单个JVM运行中唯一。重新启动程序时,再次从0开始。在不同的机器上不会是唯一的。 – Thilo
相关:http://stackoverflow.com/questions/12911163/how-to-generate-incremental-identifier-in-java?rq=1 –