payload: {"action":"opened","issue":{"url":"https://api.github.com/repos/imadcn/idworker/issues/8","repository_url":"https://api.github.com/repos/imadcn/idworker","labels_url":"https://api.github.com/repos/imadcn/idworker/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/imadcn/idworker/issues/8/comments","events_url":"https://api.github.com/repos/imadcn/idworker/issues/8/events","html_url":"https://github.com/imadcn/idworker/issues/8","id":426311365,"node_id":"MDU6SXNzdWU0MjYzMTEzNjU=","number":8,"title":"低并发情况下，雪花算法生成全是偶数问题","user":{"login":"liuxinpeister","id":7043237,"node_id":"MDQ6VXNlcjcwNDMyMzc=","avatar_url":"https://avatars0.githubusercontent.com/u/7043237?v=4","gravatar_id":"","url":"https://api.github.com/users/liuxinpeister","html_url":"https://github.com/liuxinpeister","followers_url":"https://api.github.com/users/liuxinpeister/followers","following_url":"https://api.github.com/users/liuxinpeister/following{/other_user}","gists_url":"https://api.github.com/users/liuxinpeister/gists{/gist_id}","starred_url":"https://api.github.com/users/liuxinpeister/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/liuxinpeister/subscriptions","organizations_url":"https://api.github.com/users/liuxinpeister/orgs","repos_url":"https://api.github.com/users/liuxinpeister/repos","events_url":"https://api.github.com/users/liuxinpeister/events{/privacy}","received_events_url":"https://api.github.com/users/liuxinpeister/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-03-28T05:54:57Z","updated_at":"2019-03-28T05:54:57Z","closed_at":null,"author_association":"NONE","body":"雪花算法默认实现在调用频次低的情况下，都是垮毫秒生成，导致sequence全是0，最终数据都是偶数，不利于分表计算，数据平均。是否可以按网上的方法优化一下。\r\nsequence全局自增，溢出后，等待下1ms，而不是每次都置零。\r\n\r\n\r\n\r\n`public synchronized long nextId() {\r\n        long timestamp = timeGen();\r\n\r\n        // 如果当前时间小于上一次ID生成的时间戳，说明系统时钟回退过这个时候应当抛出异常\r\n        if (timestamp < this.lastTimestamp) {\r\n            String message = String.format(\"Clock moved backwards. Refusing to generate id for %d milliseconds.\", (this.lastTimestamp - timestamp));\r\n            logger.error(message);\r\n            throw new RuntimeException(message);\r\n        }\r\n\r\n        // 对新的timestamp，sequence从0开始\r\n        this.sequence = this.sequence + 1 & this.sequenceMask;\r\n        // 毫秒内序列溢出\r\n        if (this.sequence == 0) {\r\n            // 阻塞到下一个毫秒,获得新的时间戳\r\n            timestamp = this.tilNextMillis(this.lastTimestamp);\r\n        }\r\n\r\n\r\n\r\n        this.lastTimestamp = timestamp;\r\n        // 移位并通过或运算拼到一起组成64位的ID\r\n        return timestamp - this.epoch << this.timestampLeftShift | this.workerId << this.workerIdShift | this.sequence;\r\n    }`"}}
other: {"actor":{"display_login":"liuxinpeister"}}