, , , , , , . , . - , a >= , .
private static final AtomicLong time = new AtomicLong(0);
public static long uniqueTimedId() {
while(true) {
long now = System.currentTimeMillis();
long value = time.get();
long next = now > value ? now : value + 1;
if (time.compareAndSwap(value, next))
return next;
}
}
, . , . , . , 1 .
private static final AtomicLong time = new AtomicLong(0);
public static long uniqueTimedId() {
while(true) {
long now = System.currentTimeMillis() * 1000;
long value = time.get();
long next = now > value ? now : value + 1;
if (time.compareAndSwap(value, next))
return next;
}
}
, 1000x . , 1000 , String, 1000, , x % 1000 .
10. 10 .
/ , , .
/, , .
. , volatile. .
, volatile?
synchronized , , . volatile .
private static String uniqueID;
public static synchronized String generateUniqueID() {
uniqueID = UUID.randomUUID().toString();
return uniqueID;
}
, , .
public static String generateUniqueID() {
return UUID.randomUUID().toString();
}