All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dengchen2020.id.snowflake.SnowWorker2 Maven / Gradle / Ivy

There is a newer version: 0.0.35
Show newest version
package io.github.dengchen2020.id.snowflake;

import io.github.dengchen2020.id.exception.IdGeneratorException;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 雪花算法-传统算法
 * @author dengchen
 * @since 2024/7/1
 */
class SnowWorker2 extends SnowWorker {

    public SnowWorker2(SnowflakeIdGeneratorOptions options) {
        super(options);
    }

    private static final ReentrantLock lock = new ReentrantLock();

    @Override
    public long nextId() {
        lock.lock();

        try {
            long currentTimeTick = GetCurrentTimeTick();

            if (_LastTimeTick == currentTimeTick) {
                if (_CurrentSeqNumber++ > maxSeqNumber) {
                    _CurrentSeqNumber = minSeqNumber;
                    currentTimeTick = GetNextTimeTick();
                }
            } else {
                _CurrentSeqNumber = minSeqNumber;
            }

            if (currentTimeTick < _LastTimeTick) {
                throw new IdGeneratorException("Time配置错误 {0} 毫秒", _LastTimeTick - currentTimeTick);
            }

            _LastTimeTick = currentTimeTick;

            return ((currentTimeTick << _TimestampShift) + ((long) workerId << seqBitLength) + (int) _CurrentSeqNumber);
        } finally {
            lock.unlock();
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy