summaryrefslogtreecommitdiffstats
path: root/service-monitor
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2018-12-20 14:08:42 +0100
committerHåkon Hallingstad <hakon@oath.com>2018-12-20 14:08:42 +0100
commit9922b4a419ecad5c9d4ea9e351f5e66e55fad84b (patch)
tree32abad6efa16653bacd5caaf559d456c3222a12e /service-monitor
parent58e87745267e02e051f6311024dc2fe980ec03a5 (diff)
ThreadLocalRandom is recommended over Random in multithreaded environments, try 2
Diffstat (limited to 'service-monitor')
-rw-r--r--service-monitor/src/main/java/com/yahoo/vespa/service/executor/RunletExecutorImpl.java7
1 files changed, 2 insertions, 5 deletions
diff --git a/service-monitor/src/main/java/com/yahoo/vespa/service/executor/RunletExecutorImpl.java b/service-monitor/src/main/java/com/yahoo/vespa/service/executor/RunletExecutorImpl.java
index 1f647a7fb31..1e2547cc762 100644
--- a/service-monitor/src/main/java/com/yahoo/vespa/service/executor/RunletExecutorImpl.java
+++ b/service-monitor/src/main/java/com/yahoo/vespa/service/executor/RunletExecutorImpl.java
@@ -4,10 +4,10 @@ package com.yahoo.vespa.service.executor;
import com.yahoo.log.LogLevel;
import java.time.Duration;
-import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
@@ -18,9 +18,6 @@ import java.util.logging.Logger;
public class RunletExecutorImpl implements RunletExecutor {
private static Logger logger = Logger.getLogger(RunletExecutorImpl.class.getName());
- // About 'static': Javadoc says "Instances of java.util.Random are threadsafe."
- private static final Random random = new Random();
-
private final AtomicInteger executionId = new AtomicInteger(0);
private final ConcurrentHashMap<Integer, CancellableImpl> cancellables = new ConcurrentHashMap<>();
private final ScheduledThreadPoolExecutor executor;
@@ -30,7 +27,7 @@ public class RunletExecutorImpl implements RunletExecutor {
}
public Cancellable scheduleWithFixedDelay(Runlet runlet, Duration delay) {
- Duration initialDelay = Duration.ofMillis((long) random.nextInt((int) delay.toMillis()));
+ Duration initialDelay = Duration.ofMillis((long) ThreadLocalRandom.current().nextInt((int) delay.toMillis()));
CancellableImpl cancellable = new CancellableImpl(runlet);
ScheduledFuture<?> future = executor.scheduleWithFixedDelay(cancellable, initialDelay.toMillis(), delay.toMillis(), TimeUnit.MILLISECONDS);
cancellable.setPeriodicExecutionCancellationCallback(() -> future.cancel(false));