synchronized public long generateId64 ( ) { long timestamp = System . currentTimeMillis ( ) ; long sequence = 0 ; boolean done = false ; while ( ! done ) { done = true ; while ( timestamp < lastTimestampMillisec . get ( ) ) { timestamp = waitTillNextMillisec ( timestamp ) ; } if ( timestamp == lastTimestampMillisec . get ( ) ) { // increase sequence sequence = sequenceMillisec . incrementAndGet ( ) ; if ( sequence > MAX_SEQUENCE_64 ) { // reset sequence sequenceMillisec . set ( sequence = 0 ) ; timestamp = waitTillNextMillisec ( timestamp ) ; done = false ; } } } sequenceMillisec . set ( sequence ) ; lastTimestampMillisec . set ( timestamp ) ; timestamp = ( timestamp - TIMESTAMP_EPOCH ) & MASK_TIMESTAMP_64 ; return timestamp << SHIFT_TIMESTAMP_64 | template64 | ( sequence & MASK_SEQUENCE_64 ) ; }
Generates a 64 - bit id .