使用新的Date()作为唯一标识符 [英] Using new Date() as unique identifier

查看:259
本文介绍了使用新的Date()作为唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有一个处理,每秒创建1000个实体。
对于每个这些实体,我称之为setter:

  newEntity.setDate(new Date()); 

1)2个实体有可能收到同一个日期吗?或者是否可以假定我为日期字段获得唯一的标识符效果?



2)如果问题#1的答案是:是 - 让我们做一个小的调整:
允许创建一个函数:

  public static synchronized Date getDate(){
return new Date();
}

现在可以工作吗?

  newEntity.setDate(getDate()); 

3)如何

  System.nanoTime()? 

编辑
4)如何:

  public static synchronized Date getDate(){
Thread.Sleep(1000);
return new Date();
}

谢谢。

解决方案

一个简单的测试显示,连续两次调用 new Date()可以返回相同的日期。使方法同步不会有任何区别。



如果您需要的是唯一的ID,您可以使用 AtomicInteger计数器 return counter.getAndIncrement(); 为新的ids。



ps:using System.nanotime()将无法帮助,因为分辨率是os和处理器相关的,通常足够低,两个连续的调用也可以返回相同的结果。






编辑



您的第四个提议在同步方法中睡一秒钟可能会解决你的单一性问题(尽管由yshavit指出,javadoc中没有任何保证)。请注意,使用日期作为唯一ID本身是一个坏主意:日期是可变的,所以调用代码可以使用 setTime 方法(错误或on目的)。



最后,如果你真的希望你的id与日期相关,你可以使用一个长的表示毫秒,从时代,并跟踪现有的ids如下所示:

  private static final Set< Long> usedIds = new HashSet<> (); 
public static synchronized long getUniqueId(){
long millis;
do {
millis = System.currentTimeMillis();
} while(!usedIds.add(millis));
return millis;
}


Imagine i have a proccess that creates 1000 entities each second. for each of these entities i call the setter :

newEntity.setDate(new Date());

1) Is it possible that 2 entities will recieve the same date? or is it safe to assume that i do get a unique identifier effect for the date field?

2) If the answer to question #1 is :"yes" - let's make a minor tweak: lets create a function:

public static synchronized Date getDate() {
     return new Date();
}

will it work now?

newEntity.setDate(getDate());

3) what about

System.nanoTime()?

EDIT 4) what about :

public static synchronized Date getDate() {
     Thread.Sleep(1000);
     return new Date();
}

thanks.

解决方案

A simple test shows that two consecutive calls to new Date() can return the same date. Making the method synchronized won't make any difference.

If all you need is a unique ID, you could use an AtomicInteger counter and return counter.getAndIncrement(); for new ids.

ps: using System.nanotime() won't help either as the resolution is os and processor dependent and is generally low enough that two consecutive calls can return the same result too.


EDIT

Your 4th proposal to sleep for a second in a synchronized method would probably solve your unicity issue (although as pointed out by yshavit, nothing in the javadoc guarantees it). Note however that using a Date as a unique id is a bad idea in itself: Dates are mutable so the calling code could change its id with the setTime method (by mistake or on purpose).

Finally, if you really want your id to be date related, you could use a long representing milliseconds since the epoch and keep track of the existing ids - something like this:

private static final Set<Long> usedIds = new HashSet<> ();
public static synchronized long getUniqueId() {
    long millis;
    do {
        millis = System.currentTimeMillis();
    } while (!usedIds.add(millis));
    return millis;
}

这篇关于使用新的Date()作为唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆