1
0
mirror of synced 2025-12-24 22:17:58 +08:00

限定TopOverCostCount范围

This commit is contained in:
yitter
2022-07-16 00:42:36 +08:00
parent 519206cdbf
commit b1b3d0cd6d
12 changed files with 165 additions and 84 deletions

View File

@@ -66,7 +66,8 @@ public class SnowWorkerM1 implements ISnowWorker {
SeqBitLength = options.SeqBitLength == 0 ? 6 : options.SeqBitLength;
MaxSeqNumber = options.MaxSeqNumber <= 0 ? (1 << SeqBitLength) - 1 : options.MaxSeqNumber;
MinSeqNumber = options.MinSeqNumber;
TopOverCostCount = options.TopOverCostCount == 0 ? 2000 : options.TopOverCostCount;
// TopOverCostCount = options.TopOverCostCount == 0 ? 2000 : options.TopOverCostCount;
TopOverCostCount = options.TopOverCostCount;
_TimestampShift = (byte) (WorkerIdBitLength + SeqBitLength);
_CurrentSeqNumber = MinSeqNumber;
}
@@ -150,11 +151,11 @@ public class SnowWorkerM1 implements ISnowWorker {
BeginTurnBackAction(_TurnBackTimeTick);
}
// try {
// Thread.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// try {
// Thread.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
return CalcTurnBackId(_TurnBackTimeTick);
}
@@ -214,7 +215,7 @@ public class SnowWorkerM1 implements ISnowWorker {
long tempTimeTicker = GetCurrentTimeTick();
while (tempTimeTicker <= _LastTimeTick) {
try {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
@@ -232,4 +233,3 @@ public class SnowWorkerM1 implements ISnowWorker {
}
}
}

View File

@@ -11,7 +11,6 @@ import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.core.SnowWorkerM1;
import com.github.yitter.core.SnowWorkerM2;
public class DefaultIdGenerator implements IIdGenerator {
private static ISnowWorker _SnowWorker = null;
@@ -40,7 +39,8 @@ public class DefaultIdGenerator implements IIdGenerator {
maxWorkerIdNumber = 63;
}
if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) {
throw new IdGeneratorException("WorkerId error. (range:[0, " + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63) + "]");
throw new IdGeneratorException(
"WorkerId error. (range:[0, " + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63) + "]");
}
// 4.SeqBitLength
@@ -62,6 +62,11 @@ public class DefaultIdGenerator implements IIdGenerator {
throw new IdGeneratorException("MinSeqNumber error. (range:[5, " + maxSeqNumber + "]");
}
// 7.TopOverCostCount
if (options.TopOverCostCount < 0 || options.TopOverCostCount > 10000) {
throw new IdGeneratorException("TopOverCostCount error. (range:[0, 10000]");
}
switch (options.Method) {
case 2:
_SnowWorker = new SnowWorkerM2(options);

View File

@@ -26,7 +26,7 @@ public class GenTest {
long end = System.currentTimeMillis();
long time = end - start;
System.out.println(id);
// System.out.println(id);
System.out.println("++++++++++++++++++++++++++++++++++++++++WorkerId: "
+ WorkerId + ", total: " + time + " ms");

View File

@@ -25,7 +25,7 @@ public class StartUp {
// options.BaseTime = 1582206693000L;
options.Method = method;
options.WorkerId = 1;
options.TopOverCostCount=2000;
// options.TopOverCostCount=2000;
// 首先测试一下 IdHelper 方法获取单个Id
YitIdHelper.setIdGenerator(options);
@@ -39,7 +39,7 @@ public class StartUp {
while (true) {
genTest.GenStart();
Thread.sleep(1000); // 每隔1秒执行一次GenStart
System.out.println("Hello World! Java");
// System.out.println("Hello World! Java");
}
} catch (InterruptedException e) {
e.printStackTrace();