publicOrderNoUtil(long workerId){ super(); if (workerId > this.maxWorkerId || workerId < 0) {// workid < 1024[10位:2的10次方] thrownew IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", this.maxWorkerId)); } this.workerId = workerId; }
publicsynchronizedlongnextId()throws Exception { long timestamp = this.timeGen(); if (this.lastTimestamp == timestamp) {// 如果上一个timestamp与新产生的相等,则sequence加一(0-4095循环),下次再使用时sequence是新值 //System.out.println("lastTimeStamp:" + lastTimestamp); this.sequence = this.sequence + 1 & this.sequenceMask; if (this.sequence == 0) { timestamp = this.tilNextMillis(this.lastTimestamp);// 重新生成timestamp } } else { this.sequence = 0; } if (timestamp < this.lastTimestamp) { // logger.error(String.format("Clock moved backwards.Refusing to generate id for %d milliseconds", (this.lastTimestamp - timestamp))); thrownew Exception(String.format("Clock moved backwards.Refusing to generate id for %d milliseconds", (this.lastTimestamp - timestamp))); }