content: package com.sohu.idcenter;
  
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  /**
   * generator of 19 bits number with timestamp
   *
   * @author adyliu (imxylz@gmail.com)
   * @since 2016-06-28
   */
  public class SidWorker {
  
      private static long lastTimestamp = -1L;
      private static int sequence = 0;
      private static final long MAX_SEQUENCE = 100;
      private static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  
      /**
       * 19 bits number with timestamp (20160628175532000002)
       *
       * @return 19 bits number with timestamp
       */
      public static synchronized long nextSid() {
          long now = timeGen();
          if (now == lastTimestamp) {
              if (sequence++ > MAX_SEQUENCE) {
                  now = tilNextMillis(lastTimestamp);
                  sequence = 0;
              }
          } else {
              sequence = 0;
          }
          lastTimestamp = now;
          //
          return 100L * Long.parseLong(format.format(new Date(now))) + sequence;
      }
  
      private static long tilNextMillis(long lastTimestamp) {
          long timestamp = timeGen();
          while (timestamp <= lastTimestamp) {
              timestamp = timeGen();
          }
          return timestamp;
      }
  
      private static long timeGen() {
          return System.currentTimeMillis();
      }
  
  
  }
  