Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我用net8 不生产Id cpu 暴增 #4

Open
nodyang opened this issue Oct 18, 2024 · 7 comments
Open

我用net8 不生产Id cpu 暴增 #4

nodyang opened this issue Oct 18, 2024 · 7 comments

Comments

@nodyang
Copy link

nodyang commented Oct 18, 2024

Console.WriteLine("Hello, World!");
XTrace.UseConsole();

var sf = new SnowFlake(11, 2);

for (int i = 0; i < 100; i++)
{
XTrace.WriteLine(sf.NextId().ToString());
}

@nodyang
Copy link
Author

nodyang commented Oct 18, 2024

我退回1.30 是可以的

@nodyang
Copy link
Author

nodyang commented Oct 18, 2024

image
我用源码也是这样的

@nodyang
Copy link
Author

nodyang commented Oct 18, 2024

image
一直卡在这个地方了

@nodyang
Copy link
Author

nodyang commented Oct 18, 2024

public virtual long NextId()
{
while (true)
{
long timestamp = GetNewStamp();
if (timestamp < _lastStamp)
{
// 时钟回拨,更新为上一次生成id的时间戳
timestamp = _lastStamp;
}

    if (_lastStamp == timestamp)
    {
        // 相同毫秒内,序列号自增
        _sequence = (_sequence + 1) & MaxSequence;
        // 同一毫秒的序列数已经达到最大
        if (_sequence == 0L)
        {
            // 等待下一个时间戳
            SpinWait.SpinUntil(() => (timestamp = GetNewStamp()) > _lastStamp);
        }
    }
    else
    {
        // 不同毫秒内,序列号置为0
        _sequence = 0L;
    }

    // 比较并更新 _lastStamp。只有在 _lastStamp 等于当前的 timestamp 时,才进行更新
    long originalLastStamp = Interlocked.CompareExchange(ref _lastStamp, timestamp, _lastStamp);
    if (originalLastStamp == _lastStamp)
    {
        // 成功更新时间戳,生成ID并返回
        return ((timestamp - StartStamp) << TimestampLeft)
            | (_datacenterId << DatacenterLeft)
            | (_machineId << MachineLeft)
            | _sequence;
    }
}

}

我修改后 大神指点一下

@nodyang
Copy link
Author

nodyang commented Oct 18, 2024

image
该了这个地方

@hueifeng
Copy link
Owner

可以提个PR

@nodyang
Copy link
Author

nodyang commented Oct 18, 2024

哈哈不会。技术有点漏。还是大佬给看看

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants