public synchronized long nextId() {
        long timestamp = timeGen();

        //如果当前时间小于上一次ID生成的时间戳，说明系统时钟回退过这个时候应当抛出异常
        if (timestamp < lastTimestamp) {
            throw new RuntimeException(
                    String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
        }

        //如果是同一时间生成的，则进行毫秒内序列
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            //毫秒内序列溢出
            if (sequence == 0) {
                //阻塞到下一个毫秒,获得新的时间戳
                timestamp = tilNextMillis(lastTimestamp);
            }
        }
        //时间戳改变，毫秒内序列重置
        else {
            sequence = 0L;
        }

        //上次生成ID的时间截
        lastTimestamp = timestamp;

        //移位并通过或运算拼到一起组成64位的ID
        return ((timestamp - twepoch) << timestampLeftShift) //
                | sequence;
    }
public synchronized long nextId() {
        long timestamp = timeGen();

        
        if (timestamp < lastTimestamp) {
            throw new RuntimeException(
                    String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
        }

        
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            
            if (sequence == 0) {
                
                timestamp = tilNextMillis(lastTimestamp);
            }
        }
        
        else {
            sequence = 0L;
        }

        
        lastTimestamp = timestamp;

        
        return ((timestamp - twepoch) << timestampLeftShift) 
                | sequence;
    }
public synchronized long nextId ( ) { long timestamp = timeGen ( ) ; //如果当前时间小于上一次ID生成的时间戳，说明系统时钟回退过这个时候应当抛出异常 if ( timestamp < lastTimestamp ) { throw new RuntimeException ( String . format ( "Clock moved backwards.  Refusing to generate id for %d milliseconds" , lastTimestamp - timestamp ) ) ; } //如果是同一时间生成的，则进行毫秒内序列 if ( lastTimestamp == timestamp ) { sequence = ( sequence + 1 ) & sequenceMask ; //毫秒内序列溢出 if ( sequence == 0 ) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis ( lastTimestamp ) ; } } //时间戳改变，毫秒内序列重置 else { sequence = 0L ; } //上次生成ID的时间截 lastTimestamp = timestamp ; //移位并通过或运算拼到一起组成64位的ID return ( ( timestamp - twepoch ) << timestampLeftShift ) // | sequence ; }
获得下一个ID (该方法是线程安全的)
@return SnowflakeId
获得下一个ID ( 该方法是线程安全的 )