原子操作类AtomicInteger详解
参考
https://blog.csdn.net/fanrenxiang/article/details/80623884
生成12位连续数字
for (int i = 0; i < 10; i++) {
System.out.println(extracted12());
}
private static AtomicInteger ato = new AtomicInteger(0);
public static synchronized String extracted12() {
long l = System.currentTimeMillis() / 1000;
StringBuffer s = new StringBuffer();
s.append((l + ""));
int i = ato.incrementAndGet();
if (i > 99) {
ato.getAndSet(0);
return extracted12();
}
if (i < 10) {
s.append("0");
}
s.append(i);
return s.toString();
}
结果