面试题:在分布式系统,你能想出来几种生成唯一ID的方案?(11)


       private static long sequenceBits = 12L;

       private long workerIdShift = sequenceBits;
       private long datacenterIdShift = sequenceBits + workerIdBits;
       private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
       private long sequenceMask = -1L ^ (-1L << (int)sequenceBits);

       private long lastTimestamp = -1L;
       private static object syncRoot = new object();

       public IdWorker(long workerId long datacenterId)
       {

           // sanity check for workerId
           if (workerId > maxWorkerId || workerId < 0)
           {
               throw new ArgumentException(string.Format(\"worker Id can't be greater than %d or less than 0\" maxWorkerId));

推荐阅读