没有获得唯一的时间戳

发布于 2024-12-13 22:56:52 字数 410 浏览 0 评论 0原文

我正在为要存储的对象生成唯一 ID

,我在其中包含时间戳

,但我运行了 for 循环,

    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());
    }

但我没有获得

插入的唯一 ID Thread.sleep(15) ,然后它给了我独特的价值

还有其他方法可以获得独特的价值吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

话少心凉 2024-12-20 22:56:52

我会使用简单的 longintAtomicLong.incrementAndGet 更简单,而且是线程安全的。
另一种可能性是使用 UUID .randomUUID() 但它是一个 UUID,而不是一个数值。

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.

云朵有点甜 2024-12-20 22:56:52

您的循环在迭代之间花费的时间不到 1 毫秒,因此时间戳不会改变(这就是为什么添加对 sleep 的调用会给时钟移动时间)。

您最好使用库调用(如 palacsint 建议的那样)或自己管理 UID。执行此操作的可能性包括获取最后发布的 ID 并递增,尽管这对于多线程来说存在问题。

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.

意中人 2024-12-20 22:56:52

以下是获取唯一时间戳的 3 种方法:

/** 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;
}

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;
}
初懵 2024-12-20 22:56:52

试试这个。

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文