1

I am generating unique ID for objects to be stored

i am including the timestamp in it

but i ran the for loop

    for (int i = 0; i < 100; i++) {

        long time = Calendar.getInstance().getTimeInMillis();
        st = Integer.toHexString((int) time);
        System.out.printf("%d %s %d %n", i, st, st.length());
    }

I am not getting the Unique

I inserted Thread.sleep(15) , then it is giving me unique value

is there another way i get the unique value?

1
  • 2
    If you are expecting your loop is going to take longer than a milli second per iteration then you must be used to running on a very slow machine. Nov 4, 2011 at 16:04

4 Answers 4

3

I'd use a simple long or int. AtomicLong.incrementAndGet is more simple and it's thread-safe. Another possibility is using UUID.randomUUID() but it's an UUID, not an numeric value.

1
  • 'AtomicLong.incrementAndGet() ' looks interesting At least it is better than inserting 'Thread.sleep()'
    – ajduke
    Nov 4, 2011 at 13:12
1

Your loop is taking less than 1ms between iterations, so the timestamp doesn't change (this is why adding a call to sleep gives the clock time to move).

You are probably better off using a library call, like palacsint suggested, or managing the UID yourself. Possibilities for doing this include getting the last issued ID and incrementing, although this has problems for multithreading.

0

Here are 3 methods for unique timestamp:

/** WITH LOCK */
private static long lastTs1 = Long.MIN_VALUE;
private static final long uniqueTs1() {
    long unique_ts = System.currentTimeMillis();
    synchronized (SmtpServer.class) { lastTs1 = unique_ts = unique_ts > lastTs1 ? unique_ts : lastTs1 + 1; }
    return unique_ts;
}

/** WITHOUT LOCK */
private static AtomicLong lastTs2 = new AtomicLong(Long.MIN_VALUE);
private static final long uniqueTs2() { return lastTs2.updateAndGet((v)->Math.max(v+1, System.currentTimeMillis())); }

/** WITHOUT LOCK, WITHOUT extra class */
private static AtomicLong lastTs3 = new AtomicLong(Long.MIN_VALUE);
private static final long uniqueTs() {
    long expect, next = System.currentTimeMillis();
    do {
        expect = lastTs3.get();
        if(expect >= next) next = expect + 1;
    } while(!lastTs3.compareAndSet(expect, next));
    return next;
}
0

Try this.

public class DateTimeService {

    @Autowired
    private Clock clock;
    private LocalDateTime current;

    @PostConstruct
    public void init() {
        current = LocalDateTime.now(clock);
    }

    public synchronized LocalDateTime getUniqueTimestamp() {
        LocalDateTime now = LocalDateTime.now(clock);
        if (current.isEqual(now) || current.isAfter(now)) {
            current = current.plus(1, ChronoUnit.MICROS);
        } else {
            current = now;
        }
        return current;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.