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());     }           } 可是請注意,若是將系統部署到集羣上面,狀況有會有不一樣了,不一樣的服務器集羣生成的這個數字,是有重合的機率的,所以,通常狀況是,將集羣中的每一個機器進行編碼,而後將機器編碼放在這個標識的前面以示區分。
相關文章
相關標籤/搜索