broker-game / git-broker-client

A Slow Event Messaging/Streaming client using Git for educational purposes
Apache License 2.0
1 stars 0 forks source link

Review Concurrency isolation for async calls #107

Closed jabrena closed 4 years ago

jabrena commented 4 years ago
    private long getEpoch() {
        return System.currentTimeMillis();
    }
jabrena commented 4 years ago
    private static final AtomicLong LAST_TIME_MS = new AtomicLong();

    public static long getEpoch() {
        long now = System.currentTimeMillis();
        while(true) {
            long lastTime = LAST_TIME_MS.get();
            if (lastTime >= now) {
                now = lastTime + 1;
            }
            if (LAST_TIME_MS.compareAndSet(lastTime, now)) {
                return now;
            }
        }
    }

https://stackoverflow.com/questions/9191288/creating-a-unique-timestamp-in-java/9191383

Tested alternative