java生成惟一数字

用UUID类生成惟一标识的时候,会生成一个十六进制的整数,可是不能做为数据库long型字段的惟一标识,用下面的办法能够实现数据库long型标识的生成: public class ProfileUtil {     private static AtomicInteger counter = new AtomicInteger(0);     /**      * 长生消息id      */     public static long getAtomicCounter() {         if (counter.get() > 999999) {             counter.set(1);         }         long time = System.currentTimeMillis();         long returnValue = time * 100 + counter.incrementAndGet();         return returnValue;     }     private static long incrementAndGet() {         return counter.incrementAndGet();     }     public static void main(String[] args) {                  System.out.println(ProfileUtil.getAtomicCounter());     }           } 可是请注意,若是将系统部署到集群上面,状况有会有不一样了,不一样的服务器集群生成的这个数字,是有重合的几率的,所以,通常状况是,将集群中的每一个机器进行编码,而后将机器编码放在这个标识的前面以示区分。
相关文章
相关标签/搜索