Timestamp in GlobalIdGenerator is implemented using ThreadLocalRandom (#6623)

This commit is contained in:
Kirs 2021-03-26 08:33:36 +08:00 committed by GitHub
parent 92c0cb856d
commit f3ca0d0ea5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 6 deletions

View File

@ -18,8 +18,9 @@
package org.apache.skywalking.apm.agent.core.context.ids;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.skywalking.apm.util.StringUtil;
public final class GlobalIdGenerator {
@ -58,7 +59,6 @@ public final class GlobalIdGenerator {
// Just for considering time-shift-back only.
private long runRandomTimestamp;
private int lastRandomValue;
private Random random;
private IDContext(long lastTimestamp, short threadSeq) {
this.lastTimestamp = lastTimestamp;
@ -74,11 +74,8 @@ public final class GlobalIdGenerator {
if (currentTimeMillis < lastTimestamp) {
// Just for considering time-shift-back by Ops or OS. @hanahmily 's suggestion.
if (random == null) {
random = new Random();
}
if (runRandomTimestamp != currentTimeMillis) {
lastRandomValue = random.nextInt();
lastRandomValue = ThreadLocalRandom.current().nextInt();
runRandomTimestamp = currentTimeMillis;
}
return lastRandomValue;