UUID long类型、String类型

匿名 (未验证) 提交于 2019-12-03 00:02:01
package com.test.utils;  import java.util.Random; import java.util.UUID;   public  final class UIDUtil {     private static  final int SHORT_MAX=65536;     private static int counter=-1;     private UIDUtil(){}     /**      * Creates a unique 64 bits ID by aggregating the current time in      * milliseconds since epoch (Jan. 1, 1970) and using a 16 bits counter. The      * counter is initialized at a random number. This generator can create up      * to 65536 different id per millisecond.      *      * @return a new id.      */     public static synchronized long nextId() {         long now = System.currentTimeMillis();         if (counter == -1) {             long seed = now ^ Thread.currentThread().getId();             Random rnd = new Random(Long.hashCode(seed));             counter = rnd.nextInt(SHORT_MAX);         }         long id = (now << 16) | counter;         counter = (counter + 1) % SHORT_MAX;         return id;     }      /**      * generate uniq uuid      * @return      */     public static synchronized String getUUID(){         String s= UUID.randomUUID().toString();         return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);     }  }  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!